iOS 打电话、发短信、发邮件


iPhone打电话功能:

方法一:直接拨打电话,但拨打完后不能反回到原应用界面

openURL用于打开资源,如:打电话、发短信、发邮件、跳转到其他应用、网页等

NSURL *url = [NSURL URLWithString:@"tel://123456"];
[[UIApplication sharedApplication] openURL:url];

方法二:拨打前会出现弹框提醒,拨打完后也可以反回应用界面,但这是苹果的私有API,上架软件不能使用此方法

NSURL *url = [NSURL URLWithString:@"telprompt://123456"];
[[UIApplication sharedApplication] openURL:url];

方法三:创建一个UIWebView来加载URL,拨打时有弹框提示,拨打完后能反回到拨打应用界面,推荐使用

注意:_webView不需要显示,即不需要写[self.view addSubview: _webView];否则会挡住其他界面

@property (nonatomic,  strong) UIWebView *webView;


if (_webView == nil) {
    _webView = [[UIWebView alloc] init];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://123456"]]];


          iPhone发短信功能:

           方法一:跳至系统发短信页面,但同样发完短信不回反回原应用

NSURL *url = [NSURL URLWithString:@"sms://123456"];

[[UIApplication sharedApplication] openURL:url];

  方法二:弹出发短信页面,关闭后反回原应用,推荐使用

导入系统发短信框架:

#import <MessageUI/MessageUI.h>

// 判断手机能否发短信,模拟器中不能,不写会崩溃
if (![MFMessageComposeViewController canSendText]) return;

MFMessageComposeViewController  *message = [[MFMessageComposeViewController alloc] init];

message .body = @"你好,这是我要发的短信内容";
// 设置收件人列表
message .recipients = @[@"123456", @"7890-45678"];
// 设置代理
message .messageComposeDelegate = self;
//从当前控制器跳转显示发短信界面
[self presentViewController:message   animated:YES completion:nil];


当关闭短信页面后调用的代理方法:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult: (MessageComposeResult)result
{
    // 关闭短信界面,反回原应用
    [controller dismissViewControllerAnimated:YES completion:nil];
    //result反回发送状态
    if (result == MessageComposeResultCancelled) {
         NSLog(@"取消发送");
    } else if (result == MessageComposeResultSent) {
        NSLog(@"发送成功");
    } else {
          NSLog(@"发送失败");
    }
}


iPhone发邮件功能:(类似发短信)

方法一:

NSURL *url = [NSURL URLWithString:@"mailto://123456@qq.com"];
[[UIApplication sharedApplication] openURL:url];

方法二:

// 判断手机能否发邮件,模拟器中不能,不写会崩溃
if (![MFMailComposeViewController canSendMail]) return;

MFMailComposeViewController *email= [[MFMailComposeViewController alloc] init];


// 设置邮件主题
[email  setSubject:@"约会"];
// 设置邮件内容
[email  setMessageBody:@"明天在图书馆见" isHTML:NO];
// 设置收件人列表
[email  setToRecipients:@[@"123456@qq.com"]];
// 设置抄送人列表
[email  setCcRecipients:@[@"345677@qq.com"]];
// 设置密送人列表
[email  setBccRecipients:@[@"56789@qq.com"]];

// 发送图片附件
UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *data = UIImageJPEGRepresentation(image, 0.4);
[vc addAttachmentData:data mimeType:@"image/png" fileName:@"image.png"];

//发送其他附件

//NSData *data = [NSData datawithContentsOfFile:@"file.doc"]
// 设置代理
email.mailComposeDelegate = self;
// 显示控制器
[self presentViewController:email  animated:YES completion:nil];


当关闭发邮件页面后调用的代理方法:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError *)error
{

    // 关闭邮件界面
    [controller dismissViewControllerAnimated:YES completion:nil];
     //result 反回发送状态
    if (result == MFMailComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MFMailComposeResultSent) {
               NSLog(@"发送成功");
   } else {
       NSLog(@"发送失败");
    }
   }


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值