47、[ iOS ] 调用发短信功能

作者: 天听云道 | 来源:发表于2016-07-07 18:47 被阅读580次

一、程序外调用

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://110"]];

二、程序内调用, 相比第一种用户发短信之后还可以回到App

1、导入 MessageUI.framework 框架。
2、引入头文件 #import <MessageUI/MessageUI.h> ,实现代理方法 <MFMessageComposeViewControllerDelegate> 。

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

    [self dismissViewControllerAnimated:YES completion:nil];

    switch (result) {
        case MessageComposeResultCancelled:
            NSLog(@"取消发送");
            break;
            
        case MessageComposeResultSent:
            NSLog(@"已发送");
            break;
            
        case MessageComposeResultFailed:
            NSLog(@"发送失败");
            break;
            
        default:
            break;
    }
}

3、发送短信方法

- (void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        // --phones发短信的手机号码的数组,数组中是一个即单发,多个即群发。
        controller.recipients = phones;
        // --短信界面 BarButtonItem (取消按钮) 颜色
        controller.navigationBar.tintColor = [UIColor redColor];
        // --短信内容
        controller.body = body;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }
    else
    {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                                 message:@"该设备不支持短信功能"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:alertAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}

4、发送短信方法调用

    [self showMessageView:[NSArray arrayWithObjects:@"110",@"1300000000", nil] title:@"test" body:@"来啊,快活啊,反正有大把时光"];

相关文章

网友评论

  • 多LV信源:总结的简明扼要, 赞一个.
    messageComposeViewController: didFinishWithResult: 里[self dismissViewControllerAnimated:YES completion:nil];可用 [controller dismissViewControllerAnimated:YES completion:nil];更好
    天听云道:@多LV信源 :+1:
  • 随梦而飞飞:为什么 controller.navigationBar.tintColor = [UIColor redColor]; 设置了 没有什么用啊?
    还有这个 [[[[controller viewControllers] lastObject] navigationItem] setTitle:title]; 其中title 也没有用啊?
    天听云道:@随梦而飞飞 第二个方法没问题,因为这个controller是一个navigationController,找到其中的目前的ViewController,然后设置title,但在这我也不知道为啥不能用。这篇文章我也是整理网上的内容。。
    天听云道:@随梦而飞飞 第一个是BarButtonItem的颜色,比如那个取消按钮,第二个确实有问题,我删了在研究研究。
  • 随梦而飞飞:代理方法中的 [self dismissViewControllerAnimated:YES completion:nil];是什么 意思啊~~就一个控制器 怎么 跳回去啊
    随梦而飞飞:@天听云道 除非往下滑 才能得下去 我用的搜狗输入法
    随梦而飞飞:@天听云道 哦 已经 知道原理和 调用顺序了 只是跳转到短信界面 键盘是自动弹出的 而且还弄不下去
    天听云道:@随梦而飞飞 发短信界面就是模态present弹出的,跳回去就是dismiss啊
  • 随梦而飞飞:楼主 给个 Demo啊~~~为什么总是 弄不好啊~~

本文标题:47、[ iOS ] 调用发短信功能

本文链接:https://www.haomeiwen.com/subject/hpuljttx.html