iOS-----MFMessageCompose 和 MFMailComposeViewController的使用方法

MFMessageCompose 和 MFMailComposeViewController的使用方法

使用MFMessageComposeViewCOntroller发短信

应用想自己提供界面让用户输入短信收件人地址、短信内容、主体、附件等短信内容,则可使用MFMessageComposeViewController来发送短信,它也是一个视图控制器,继承UINavigationController.

MFMessageComposeViewController提供了如下类方法判断iOS设备是否支持发送短信.


+ canSendText:


该iOS设备是否支持发送文本短信.


+ canSendAttachments:


该iOS设备是否支持发送带附件的短信


+ canSendSubject:


该iOS设备是否支持发送带标题的短信


程序使用MFMessageComposeViewController的类方法进行判断之后,接下来就可按如下步骤发送短信.


1


创建MFMessageComposeViewController对象


2


为MFMessageComposeViewController设置recipients(接受NSArray作为属性值,用于设置多个收件人号码)、subject(设置短信主题)、body(设置短信内容)、attachments(接受NSArray作为属性值,用于设置多个附件)等属性


3


为MFMessageComposeViewController设置messageComposeDelegate,该属性值必须是一个实现MFMessageComposeViewControllerDelegate协议的对象.该协议中定义了一个必须实现的messageComposeViewComtroller:didFinishWithResult:方法,该方法负责处理短信的发送结果.


  1 @interface LCViewController()<MFMessageComposeViewControllerDelegate>
  2
  3 @end
  4
  5 @implementation LCViewController
  6
  7 - (void)viewDidLoad
  8
  9 {
 10
 11    [super viewDidLoad];
 12
 13 }
 14
 15 - (IBAction)send:(id)sender
 16
 17 {
 18
 19    NSString* destStr = self.destField.text;
 20
 21    NSString* contentStr = self.contentField.text;
 22
 23    if(destStr != nil && destStr.length>0 && contentStr != nil && destStr.length > 0 )
 24
 25    {
 26
 27        // 如果能发送文本信息
 28
 29       if([MFMessageComposeViewController canSendText])
 30
 31       {
 32
 33 // 创建MFMessageComposeViewController对象
 34
 35 MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
 36
 37 // 为MFMessageComposeViewController对象指定messageComposeDelegate
 38
 39 picker.messageComposeDelegate = self;
 40
 41 picker.navigationBar.tintColor = [UIColor blackColor];
 42
 43 // 设置收件人,此处可通过NSArray集合指定多个收件人
 44
 45 picker.recipients = [NSArray arrayWithObject:destStr];
 46
 47 // 设置短信内容
 48
 49 picker.body = contenStr;
 50
 51 /*
 52
 53   如果运营商支持,picker还支持指定subjecy(主题)和attachments(附件)
 54
 55   也可用addAttachmentURL:withAlternateFilename:或addAttachmentData:typeIdentifier:filename:方法添加附件
 56
 57 */
 58
 59 // 显示MFMessageComposeViewController控制器
 60
 61 [self persentViewController:picker animated:YES completion:nil];
 62
 63 }
 64
 65 }
 66
 67 }
 68
 69 //  MFMessageComposeViewControllerDelegate协议中的方法,负责处理短信的发送结果
 70
 71 - (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result
 72
 73 {
 74
 75 switch(result)
 76
 77 {
 78
 79   case MessageComposeResultCancelled:
 80
 81       [self showAlert:@”结果: 短信被取消发送”];
 82
 83       break;
 84
 85   case MessageComposeResultSent:
 86
 87       [self showAlert:@”结果: 发送成功”];
 88
 89       break;
 90
 91   case MessageComposeResultFailed:
 92
 93       [self showAlert:@”结果: 发送失败”];
 94
 95         break;
 96
 97    default:
 98
 99       [self showAlert:@”结果: 没有发送短信”];
100
101     break;
102
103 }
104
105 [self dismissViewControllerAnimated:YES completion:nil];
106
107 }
108
109 - (void)showAlert:(NSString *)msg
110
111 {
112
113 [[[UIAlertView alloc] initWithTitle:@”发送结果” message:msg delegate:nil cancelButtonTitle:@”确 定” otherButtonTitles:nil] show];
114
115 }
116
117 @end

创建了一个MFMessageComposeViewController对象,并为该对象设置了recipents(收件人)、body(短信内容),并将该视图控制器本身设为它的messageComposeDelegate,因此该视图控制器类实现了MFMessageComposeViewControllerDelegate协议,并实现该协议中的方法----该方法负责处理发送短信的结果。

     

使用MFMailComposeViewController发送邮件

MFMailComposeViewController与MFMessageComposeViewController的用法非常相似,只是功能不同而已------MFMailComposeViewController用于发送邮件。

MFMailComposeViewController提供了如下类方法判断iOS设备是否支持发送邮件。

+ canSendMail: 该iOS设备是否支持发送邮件.

程序使用MFMailComposeViewController的类方法进行判断之后,接下来就可按如下步骤发送邮件.

  1. 创建MFMailComposeViewController对象
  2. 为MFMailComposeViewController设置toRecipients: (接受NSArray作为属性值,用于设置多个收件人地址)、ccRecipients:(接受NSArray作为属性值,用于设置多个抄送人地址)、bccRecipients:(接收NSArray作为属性值,用于设置多个密送人地址)、subject(设置邮件主题),还可通过setMessageBody:isHTML:方法设置邮件正文,通过addAttachmentData:mimeType:filename:方法添加附件.
  3. 为MFMailComposeViewController设置mailComposeDelegate,该属性值必须是一个实现MFMailComposeViewControllerDelegate协议的对象.该协议中定义了一个必须实现的mailComposeController:didFinishWithResult:error:方法,该方法负责处理邮件的发送结果.

  1 @interface LCViewController()<MFMailComposeViewControllerDelegate>
  2
  3 @end
  4
  5 @implementation LCViewController
  6
  7 -(void)viewDidLoad
  8
  9 {
 10
 11    [super viewDidLoad];
 12
 13 }
 14
 15 -(IBAction)sendMail:(id)sender
 16
 17 {
 18
 19   // 获取界面上用户输入的内容
 20
 21   NSString* toStr = self.toField.text;// 收件人地址
 22
 23   NSString* ccStr = self.ccField.text;// 抄送人地址
 24
 25   NSString* bccStr = self.bccField.text;// 密送人地址
 26
 27   NSString* subjectStr = self.subjectField.text;// 邮件主题
 28
 29 NSString* contentStr = self.contentField.text;// 邮件正文
 30
 31 if(toStr != nil && toStr.length > 0
 32
 33 && subjectStr != nil && subjectStr.length > 0
 34
 35 && contentStr != nil && contentStr.length > 0)
 36
 37 {
 38
 39   // 如果能发送邮件
 40
 41   if([MFMailComposeViewController canSendMail])
 42
 43 {
 44
 45   // 创建MFMailComposeViewController对象
 46
 47  MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]  init];
 48
 49 // 为 MFMailComposeViewController对象指定mailComposeDelegate
 50
 51 picker.mailComposeDelegate = self;
 52
 53 picker.navigationBar.tintColor = [UIColor blackColor];
 54
 55 // 设置收件人,此处可通过NSArray集合指定多个收件人
 56
 57 picker.toRecipients = [NSArray arrayWithObject:toStr];
 58
 59 if(ccStr != nil && ccStr.length > 0)
 60
 61 {
 62
 63   // 设置抄送人,此处可通过NSArray集合指定多个抄送人
 64
 65  picker.ccRecipients = [NSArray arrayWithObject:ccStr];
 66
 67 }
 68
 69 if(bccStr != nil && bccStr.length > 0)
 70
 71 {
 72
 73   // 设置密送人,此处可用过NSArray集合指定多个密送人
 74
 75   picker.bccRecipients = [NSArray arrayWithObject:bccStr];
 76
 77 }
 78
 79 // 设置邮件主题
 80
 81 picker.subject = subjectStr;
 82
 83 // 设置邮件正文
 84
 85 [picker setMessageBody:contentStr isHTML:NO];
 86
 87 // 显示MFMailComposeViewController控制器
 88
 89 [self persentViewController:picker animated:YES completion:nil];
 90
 91 }
 92
 93 }
 94
 95 }
 96
 97 -(IBAction)finishEdit:(id)sender
 98
 99 {
100
101   [sender resignFirstResponder];
102
103 }
104
105 -(void)mailComposeController:(MFMailComposeViewController*)controller
106
107   didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
108
109 {
110
111   switch(result)
112
113   {
114
115 case  MFMailComposeResultCancelled:
116
117      [self showAlert:@”结果: 邮件被取消发送”];
118
119      break;
120
121 case  MFMailComposeResultSent:
122
123     [self showAlert:@”结果: 发送成功”];
124
125     break;
126
127 case  MFMailComposeResultFailed:
128
129     [self showAlert:@”结果: 发送失败”];
130
131     break;
132
133 case  MFMailComposeResultSaved:
134
135     [self showAlert:@”结果: 邮件被保存了”];
136
137     break;
138
139 }
140
141 [self dismissViewControllerAnimated:YES completion:nil];
142
143 }
144
145 -(void)showAlert:(NSString *)msg
146
147 {
148
149   [ [ [ UIAlertView alloc] initWithTitle:@”发送结果”  message:msg delegate:nil cancelButtonTitle:@”确定” otherButtonTitles:nil] show];
150
151 }
152
153 @end

// 上面程序中的粗体字代码创建了一个MFMailComposeViewController对象,并为该对象设置了toRecipients(收件人地址)、ccRecipients(抄送人地址)、bccRecipients(密送人地址),还调用了setMessageBody:contentStr isHTML:方法设置邮件正文,并将该视图控制器本身设为它的mailComposeDelegate,因此该视图控制器类实现MFMailComposeViewControllerDelegate协议,并实现了该协议中的方法----该方法负责处理发送邮件的结果。

// 编译、运行该程序(必须在真机中运行,模拟器不支持),在程序界面中输入收件人地址、抄送地址、密送人地址、邮件主题、邮件正文,然后单击“发送”按钮,将可以看到如下图所示的界面

时间: 2024-08-28 12:48:54

iOS-----MFMessageCompose 和 MFMailComposeViewController的使用方法的相关文章

IOS发送Email的两种方法

IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController.借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能. 1.openURL 使用openURL调用系统邮箱客户端是我们在IOS3.0以下实现发邮件功能的主要手段.我们可以通过设置url里的相关参数来指定邮件的内容,不过其缺点很明显,这样的过程会导致程序暂时退出.下面是使用openURL来发邮件的一个小例子: #pragma mark - 使用系统邮件客户

iOS开发中自定义字体的方法

http://www.cnblogs.com/iyou/archive/2014/05/25/3751669.html 1. 首先下载你想要设置的字体库,例如设置方正启体简体 2. 添加到工程,一定要注意勾选红色框框处,默认是不勾选的  添加以后 3.在plist文件中添加 4.现在已经添加成功了,但是要使用就必须知道FontName,用以下代码可查到 NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyName

iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?

Apple官方的文档为生成一个UIImage对象提供了两种方法: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路径. 那么两种有什么区别吗? 肯定是有的.根据Apple的官方文档: imageNamed: 这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象如果它存在的话.如果缓存中没有找到相应的图片,这个方法从指定的文档中加载然后缓存并返回这个对象.因此imageNamed的优点是当加载时会缓存图片.所以当图

[编写高质量iOS代码的52个有效方法](三)消息和运行期

[编写高质量iOS代码的52个有效方法](三)消息和运行期 参考书籍:<Effective Objective-C 2.0> [英] Matt Galloway 先睹为快 11.理解objc_msgSend的作用 12.理解消息转发机制 13.用"方法调配技术"调试"黑盒方法" 14.理解"类对象"的用意 目录 编写高质量iOS代码的52个有效方法三消息和运行期 先睹为快 目录 第11条理解objc_msgSend的作用 第12条理解

[编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD)

[编写高质量iOS代码的52个有效方法](十)Grand Central Dispatch(GCD) 参考书籍:<Effective Objective-C 2.0> [英] Matt Galloway 先睹为快 41.多用派发队列,少用同步锁 42.多用GCD,少用performSelector系列方法 43.掌握GCD及操作队列的使用时机 44.通过Dispatch Group机制,根据系统资源状况来执行任务 45.使用dispatch_once来执行只需要运行一次的线程安全代码 46.不

使用多字节字符集的跨平台(PC、Android、IOS、WP)编码/解码方法

随着移动端的发展,跨平台已成为通讯架构设计的重要考虑因素,PC.Android.IOS.WP等跨多平台间的数据通讯,必然要解决字符编码/解码的问题. 多字节字符集MBCS不是跨平台的首选字符集,面向跨平台.国际化的推荐字符集肯定是UNICODE. 写VC的人都知道,在以前VC++6.0中默认的字符集是多字节字符集,而VS2005及以后默认的字符集是Unicode,VS2013中默认不再对多字节字符串进行支持. 但对很多较早的服务端项目,依然使用的是多字节字符集,不过使用多字节字符集依然可以实现跨

IOS 应用 退出的一个小方法

AppDelegate * app=(AppDelegate *)[[UIApplication sharedApplication]delegate]; UIWindow *window = app.window; [UIView animateWithDuration:1.0f animations:^{ window.alpha = 0; window.frame = CGRectMake(window.bounds.size.width/2.0, window.bounds.size.w

ios runtime 动态向类添加方法

1.定义C函数: void dynamicMethodIMP(id self, SEL _cmd) { NSLog(@"蜗牛也疯狂"); } 2.重写函数+(BOOL)resolveInstanceMethod:(SEL)sel +(BOOL)resolveInstanceMethod:(SEL)sel { class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "[email protected]:");

[编写高质量iOS代码的52个有效方法](十一)系统框架

[编写高质量iOS代码的52个有效方法](十一)系统框架 参考书籍:<Effective Objective-C 2.0> [英] Matt Galloway 先睹为快 47.熟悉系统框架 48.多用块枚举,少用for循环 49.对自定义其内存管理语义的容器使用无缝桥接 50.构建缓存时选用NSCache而非NSDictionary 51.精简initialize与load的实现代码 52.别忘了NSTimer会保留其目标对象 目录 编写高质量iOS代码的52个有效方法十一系统框架 先睹为快