IOS 调发短信和邮件界面
2015-08-28 22:06
330 查看
苹果官方demo下载地址:https://developer.apple.com/library/ios/samplecode/MessageComposer/Introduction/Intro.html
发短信比较简单:
1.首先倒入系统库:MessageUI.framework CoreGraphics.framework
2. 实现代理
MFMailComposeViewControllerDelegate, ----发邮件
MFMessageComposeViewControllerDelegate,---发短信
UINavigationControllerDelegate
3. 实现:(包括发短信和发邮件)
以下代码为官方的demo中的demo
发短信比较简单:
1.首先倒入系统库:MessageUI.framework CoreGraphics.framework
2. 实现代理
MFMailComposeViewControllerDelegate, ----发邮件
MFMessageComposeViewControllerDelegate,---发短信
UINavigationControllerDelegate
3. 实现:(包括发短信和发邮件)
以下代码为官方的demo中的demo
#pragma mark - Compose Mail/SMS // ------------------------------------------------------------------------------- // displayMailComposerSheet // Displays an email composition interface inside the application. // Populates all the Mail fields. // ------------------------------------------------------------------------------- - (void)displayMailComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Hello from California!"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; [picker setToRecipients:toRecipients]; [picker setCcRecipients:ccRecipients]; [picker setBccRecipients:bccRecipients]; // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"]; // Fill out the email body text NSString *emailBody = @"It is raining in sunny California!"; [picker setMessageBody:emailBody isHTML:NO]; [self presentViewController:picker animated:YES completion:NULL]; } // ------------------------------------------------------------------------------- // displayMailComposerSheet // Displays an SMS composition interface inside the application. // ------------------------------------------------------------------------------- - (void)displaySMSComposerSheet { MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; picker.messageComposeDelegate = self; // You can specify one or more preconfigured recipients. The user has // the option to remove or add recipients from the message composer view // controller. <span style="color:#ff0000;"> /* picker.recipients = @[@"Phone number here"]; */ 此处可以填写需要发送的手机号码</span> // You can specify the initial message text that will appear in the message // composer view controller. <span style="color:#ff0000;"> picker.body = @"Hello from California!"; 短信内容</span> <span style="color:#ff0000;"> 打开短信界面时,就会看到短信内容和手机号码自动填充好了。</span> [self presentViewController:picker animated:YES completion:NULL]; } #pragma mark - Delegate Methods // ------------------------------------------------------------------------------- // mailComposeController:didFinishWithResult: // Dismisses the email composition interface when users tap Cancel or Send. // Proceeds to update the message field with the result of the operation. // ------------------------------------------------------------------------------- - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { self.feedbackMsg.hidden = NO; // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: self.feedbackMsg.text = @"Result: Mail sending canceled"; break; case MFMailComposeResultSaved: self.feedbackMsg.text = @"Result: Mail saved"; break; case MFMailComposeResultSent: self.feedbackMsg.text = @"Result: Mail sent"; break; case MFMailComposeResultFailed: self.feedbackMsg.text = @"Result: Mail sending failed"; break; default: self.feedbackMsg.text = @"Result: Mail not sent"; break; } [self dismissViewControllerAnimated:YES completion:NULL]; } // ------------------------------------------------------------------------------- // messageComposeViewController:didFinishWithResult: // Dismisses the message composition interface when users tap Cancel or Send. // Proceeds to update the feedback message field with the result of the // operation. // ------------------------------------------------------------------------------- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { self.feedbackMsg.hidden = NO; // Notifies users about errors associated with the interface switch (result) { case MessageComposeResultCancelled: self.feedbackMsg.text = @"Result: SMS sending canceled"; break; case MessageComposeResultSent: self.feedbackMsg.text = @"Result: SMS sent"; break; case MessageComposeResultFailed: self.feedbackMsg.text = @"Result: SMS sending failed"; break; default: self.feedbackMsg.text = @"Result: SMS not sent"; break; } [self dismissViewControllerAnimated:YES completion:NULL]; }
相关文章推荐
- nagios维护之常见问题
- iOS 多线程初学 - 多种方法创建多线程
- nagios维护之添加监控
- AutoLayout -Masonry
- iOS:制作九宫格
- IOS中沙盒的基本介绍
- 斯坦福大学iOS应用开发教程学习笔记(第二课) 计算器实现(mvc实战)
- iOS-数据持久化(一)
- 高仿煎蛋iOS项目基础配置(2)
- IOS开发错误提示原因集合-----长期更新
- iOS 创建单例的两种方法
- iOS 开发流程笔记
- ios Block解决循环引用和回传值
- Xamarin iOS+MvvmCross TableCell add Button
- Xamarin IOS SearchBar 文本框设置
- iOS 代理 协议
- xamarin ios XibFree LinearLayout
- IOS编程中设置系统默认语言的方法
- iOS多线程实现3-GCD
- iOS学习 - tableViewCell(团购)