版权声明:本文为博主原创文章,未经博主允许不得转载。
需要引入框架:
MessageUI.framework
布局如下:
短信和邮件:
[objc] view plain copy
- #import "ViewController.h"
- #import <MessageUI/MessageUI.h>
- @interface ViewController ()<MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>//遵循协议
- @end
- @implementation ViewController
短信功能:
[objc] view plain copy
- //短信功能
- - (IBAction)messageButtonAction:(UIButton *)sender {
- #pragma mark 程序外发送短信
- /*
- //定义打开短信的url, 关键字: sms:
- NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",@"10086"]];
- //判断程序是否可以调用打开短信功能
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url];
- }else{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持此短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- [alert show];
- }
- */
- /*
- 用openURL来打开程序中的短信功能, 需要用到关键字: "sms:", 后面加上要发送的电话就可以了;
- 缺点:1.这个方法会跳出我们正在运行的程序,打开系统的短信界面, 但当用户关闭短信后, 无法回到程序.
- 2.这个方法我们只能定义要发送的手机号, 无法编辑发送的短信内容;
- */
- }
#pragma mark 程序内发送短信
/*
为了弥补上述的两个方法的不足,需要另一种使用短信功能的方法:程序内使用短信功能.
*/
//1.添加短信所需要的框架: MessageUI.framework
//2.引入头文件,实现如下代码
//3.判断是否可以发短信
[objc] view plain copy
- - (IBAction)messageButtonAction:(UIButton *)sender {
- #pragma mark 程序外发送短信
- BOOL canSendMessage = [MFMessageComposeViewController canSendText];
- if (canSendMessage) {
- //创建短信视图控制器
- MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc]init];
- //设置代理
- messageVC.messageComposeDelegate = self;
- //设置短信内容
- messageVC.body = @"来一条信息";
- //设置电话, 是一个数组, 可以设置多个电话, 实现群发功能
- messageVC.recipients = @[@"10086",@"10010"];
- //打开短信功能, 通过这个方法会在程序内打开一个短信界面;
- [self presentViewController:messageVC animated:YES completion:nil];
- }else{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持此短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- }
信息的代理方法:
[objc] view plain copy
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
- //MessageComposeResult 的枚举值:
- // MessageComposeResultCancelled, //取消发送短信功能
- // MessageComposeResultSent, //发送短信
- // MessageComposeResultFailed //发送失败
- if (result == MessageComposeResultCancelled || result == MessageComposeResultSent) {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- }
邮件功能:
[objc] view plain copy
- //邮件功能
- - (IBAction)mailButtonAction:(UIButton *)sender {
- #pragma mark 程序外发送邮件
- /*
- //打开系统邮件页面, mailto:
- NSURL *mailURL = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@",@"13683799303@163.com"]];
- //cc:抄送对象 subject:主题 body:内容
- //NSURL *mailURL2 = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?cc = %@&subject = %@&body = %@",@"13683799303@163.com",@"13683799303@26.com",@"邮件",@"你好啊!"]];
- if ([[UIApplication sharedApplication] canOpenURL:mailURL]) {
- [[UIApplication sharedApplication] openURL:mailURL];
- }else{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- [alert show];
- }
- */
- /*
- 此方法来发送邮件同上述短信一样,也会跳出程序,调用系统的邮件界面;
- */
- #pragma mark 程序内发送邮件
- //判断是否可以发送邮件
- BOOL canSendMail = [MFMailComposeViewController canSendMail];
- if (canSendMail) {
- //创建邮件视图控制器
- MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc]init];
- //设置接收邮件人, 数组,可以实现群发
- [mailVC setToRecipients:@[@"13683799303@163.com",@"135895587@qq.com"]];
- //设置抄送对象,
- [mailVC setCcRecipients:@[@"13683799303@163.com",@"135895587@qq.com"]];
- //设置密送
- [mailVC setBccRecipients:@[@"13683799303@163.com"]];
- //设置内容
- [mailVC setMessageBody:@"很高兴认识你" isHTML:NO];
- //设置代理
- mailVC.mailComposeDelegate = self;
- //打开邮件功能
- [self presentViewController:mailVC animated:YES completion:nil];
- }else{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- }
邮件代理的方法:
[objc] view plain copy
- - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
- // MFMailComposeResultCancelled, 取消发送
- // MFMailComposeResultSaved, 保存
- // MFMailComposeResultSent, 发送
- // MFMailComposeResultFailed 发送失败
- switch (result) {
- case MFMailComposeResultCancelled:
- NSLog(@"取消发送");
- break;
- case MFMailComposeResultSaved:
- NSLog(@"保存");
- break;
- case MFMailComposeResultSent:
- NSLog(@"发送成功");
- break;
- case MFMailComposeResultFailed:
- NSLog(@"失败");
- break;
- default:
- break;
- }
- [self dismissViewControllerAnimated:YES completion:nil];
- }
最终效果:(由于模拟器没法演示发送短信,所以会出现下面的现象)
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
原文地址:http://blog.csdn.net/qq_31810357/article/details/49990635
时间: 2024-10-03 14:11:34