美文网首页
iOS,在程序中发送短信和打电话

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

作者: zhouios | 来源:发表于2017-05-15 11:12 被阅读0次

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

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

    二,在程序内发送短信
    首先导入头文件,#import <MessageUI/MessageUI.h>和代理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,在程序中发送短信和打电话

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