iOS,在程序中发送短信和打电话

1,发送短信
发送短信有两种方式:
一,在程序外发送短信:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://156********"] options:@{} completionHandler:nil];

二,在程序内发送短信
首先导入头文件,#import 和代理MFMessageComposeViewControllerDelegate

/**
 发送短信的方法

 @param phones 手机号
 @param title 标题
 @param content 默认短信内容
 */
-(void)sendMessage:(NSArray *)phones title:(NSString *)title content:(NSString *)content
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = content;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
//        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
        
        [[[[[controller viewControllers] lastObject] navigationItem] rightBarButtonItem] setTitle:@"返回"];
    }
    else
    {
        UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"该设备不支持短信功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canlce = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [altervc addAction:canlce];
        [self presentViewController:altervc animated:YES completion:nil];
    }
}
/**
 MFMessageComposeViewControllerDelegate代理方法
 */
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
            
            break;
        case MessageComposeResultFailed:
            //信息传送失败
            
            break;
        case MessageComposeResultCancelled:
            //信息被用户取消传送
            
            break;
        default:
            break;
    }
}
//程序内调用短信,手机号可以是多个
[self sendMessage:[NSArray arrayWithObjects:@"156********", nil] title:@"标题" content:nil];

2,打电话
方式一,有弹框提示

    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"156********"];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];

方式二,有弹框提示

NSString *str = [NSString stringWithFormat:@"telprompt://156********"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
或者
NSString *str = [NSString stringWithFormat:@"tel://156********"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];

你可能感兴趣的:(iOS,在程序中发送短信和打电话)