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

一、程序外调用

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

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

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

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

你可能感兴趣的:(47、[ iOS ] 调用发短信功能)