打电话、发短信、发邮件算是比较APP小的功能,今天来总结下。
一、添加框架<MessageUI/MessageUI.h>
二、
//
// ViewController.m
// ServiceSystem
//
// Created by City--Online on 15/5/15.
// Copyright (c) 2015年 XQB. All rights reserved.
//
#import "ViewController.h"
#import <MessageUI/MessageUI.h>
@interface ViewController ()<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//发送短信
UIButton *btnSendMessage=[UIButton buttonWithType:UIButtonTypeSystem];
[btnSendMessage setTitle:@"发送短信" forState:UIControlStateNormal];
btnSendMessage.frame=CGRectMake(100, 100, 80, 50);
[btnSendMessage addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btnSendMessage.tag=10001;
[self.view addSubview:btnSendMessage];
//发送邮件
UIButton *btnSendMail=[UIButton buttonWithType:UIButtonTypeSystem];
[btnSendMail setTitle:@"发送邮件" forState:UIControlStateNormal];
btnSendMail.frame=CGRectMake(200, 100, 80, 50);
[btnSendMail addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btnSendMail.tag=10002;
[self.view addSubview:btnSendMail];
//打电话
UIButton *btnTel=[UIButton buttonWithType:UIButtonTypeSystem];
[btnTel setTitle:@"打电话" forState:UIControlStateNormal];
btnTel.frame=CGRectMake(300, 100, 80, 50);
[btnTel addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btnTel.tag=10003;
[self.view addSubview:btnTel];
}
-(void)btnClick:(UIButton *)sender
{
UIButton *btn=(UIButton *)sender;
if (btn.tag==10001) {//短信
// 1.不能指定短信内容,而且不能自动回到原应用
// NSURL *url = [NSURL URLWithString:@"sms://18575533040"];
// [[UIApplication sharedApplication] openURL:url];
// 2.使用MessageUI框架
if (![MFMessageComposeViewController canSendText]) {
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"不支持短信发送" message:@"该设备不支持发送短信功能" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:nil, nil];
[alertView show];
return;
}
MFMessageComposeViewController *messageVc=[[MFMessageComposeViewController alloc]init];
messageVc.messageComposeDelegate=self;
//接收人
messageVc.recipients=@[@"18575533040",@"10010"];
//短信内容
messageVc.body=@"body";
//短信主题
messageVc.subject=@"subject";
//模态视图
[self presentViewController:messageVc animated:YES completion:nil];
}
else if (btn.tag==10002) //邮件
{
MFMailComposeViewController *mailVc=[[MFMailComposeViewController alloc]init];
[mailVc setSubject:@"主题"];
//收件人
[mailVc setToRecipients:@[@"991843897@qq.com"]];
//抄送
[mailVc setCcRecipients:@[@"991843897@qq.com"]];
//密送
[mailVc setBccRecipients:@[@"991843897@qq.com"]];
//正文
[mailVc setMessageBody:@"这是我的<font color=\"blue\">工作报告</font>,请审阅!" isHTML:YES];
//附件
UIImage *img=[UIImage imageNamed:@"email.png"];
NSData *data=UIImagePNGRepresentation(img);
//Data二进制数据 mimeType 使用什么应用程序打开附件 fileName收件人接受时看到的附件名 可以添加多个附件
[mailVc addAttachmentData:data mimeType:@"image/png" fileName:@"图标.png"];
//设置代理
[mailVc setMailComposeDelegate:self];
//显示控制器
[self presentViewController:mailVc animated:YES completion:nil];
}
else if (btn.tag==10003) //电话
{
// 打电话有3种方式:
//// 1.不回应用 直接拨号,拨号完成后停留在通话记录中
// NSURL *telurl=[NSURL URLWithString:@"tel://18575533040"];
// [[UIApplication sharedApplication]openURL:telurl];
//// 2.出去打电话然后回来
// telprompt协议属于苹果的私有协议,一旦程序中使用了此协议,程序无法上架 针对越狱的机器开发的系统,可以使用此协议
// NSURL *telPromptUrl=[NSURL URLWithString:@"telprompt://18575533040"];
// [[UIApplication sharedApplication]openURL:telPromptUrl];
// 3.借助WebView打电话
UIWebView *webView=[[UIWebView alloc]init];
webView.frame=self.view.bounds;
[self.view addSubview:webView];
if (webView) {
NSURL *url=[NSURL URLWithString:@"tel://18575533040"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
}
}
//短信代理
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
// enum MessageComposeResult {
// MessageComposeResultCancelled, 取消
// MessageComposeResultSent, 发送
// MessageComposeResultFailed 失败
// };
// typedef enum MessageComposeResult MessageComposeResult;
NSLog(@"%d",result);
//移除
[self dismissViewControllerAnimated:YES completion:nil];
}
//邮件代理
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// enum MFMailComposeResult {
// MFMailComposeResultCancelled, 取消
// MFMailComposeResultSaved, 保存
// MFMailComposeResultSent, 发送
// MFMailComposeResultFailed 失败
// };
// typedef enum MFMailComposeResult MFMailComposeResult;
NSLog(@"%d",result);
if (result==2) {
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"发送邮件通知" message:@"发送邮件成功" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:nil, nil];
[alertView show];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end