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

一、程序外调用

[[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:@"来啊,快活啊,反正有大把时光"];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS调用系统的发短信功能可以分为两种: 1,程序外调用系统发短信。 2,程序内调用系统发短信。第二种的好处是用户...
    shalapovar阅读 2,003评论 1 0
  • 1,NSObject中description属性的意义,它可以重写吗?答案:每当 NSLog(@"")函数中出现 ...
    eightzg阅读 4,184评论 2 19
  • 一、系统应用 在开发某些应用时,我们可能希望能够调用iOS系统内置的电话、短信、邮件、浏览器应用,或者直接调用安装...
    执着丶执念阅读 1,673评论 8 25
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,868评论 18 139
  • 吴先生和罗小姐是在大学时在一起的。 开始罗小姐并不喜欢吴先生,她应该有心上人吧。只是当那天打开自己得抽屉,发现里面...
    石砳阅读 651评论 0 0