关闭

iOS中:最完善的:打电话和发短信

174人阅读 评论(0) 收藏 举报

// 打电话合法:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    UIWebView*callWebview =[[UIWebView alloc]init];

    NSURL *telURL =[NSURLURLWithString:@"tel:18679824696"];

    [callWebview loadRequest:[NSURLRequestrequestWithURL:telURL]];

    //记得添加到view上

    [self.view addSubview:callWebview];

   

}

 

// 发短信:

 

需要MessageUI.framework框架

遵守MFMessageComposeViewControllerDelegate协议

 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self showMessageView];

}

 

-(void)showMessageView

{

   

    if( [MFMessageComposeViewControllercanSendText] ){

       

        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc]init]; //autorelease];

       

        controller.recipients = [NSArrayarrayWithObject:@"18679824696"];

        controller.body =@"测试发短信";

        controller.messageComposeDelegate =self;

       

        [selfpresentModalViewController:controller animated:YES];

       

        [[[[controllerviewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题

    }else{

       

        [selfalertWithTitle:@"提示信息"msg:@"设备没有短信功能"];

    }

}

 

 

//MFMessageComposeViewControllerDelegate

 

-(void)messageComposeViewController:(MFMessageComposeViewController *)controllerdidFinishWithResult:(MessageComposeResult)result{

   

    [controllerdismissModalViewControllerAnimated:NO];//关键的一句   不能为YES

   

    switch ( result ) {

           

        case MessageComposeResultCancelled:

           

            [selfalertWithTitle:@"提示信息"msg:@"发送取消"];

            break;

        case MessageComposeResultFailed:// sendfailed

            [selfalertWithTitle:@"提示信息"msg:@"发送成功"];

            break;

        case MessageComposeResultSent:

            [selfalertWithTitle:@"提示信息"msg:@"发送失败"];

            break;

        default:

            break;

    }

}

 

 

- (void)alertWithTitle:(NSString *)title msg:(NSString *)msg {

   

   

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title

                                                   message:msg

                                                  delegate:self

                                         cancelButtonTitle:nil

                                         otherButtonTitles:@"确定", nil];

   

    [alert show]; 

   

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1728次
    • 积分:60
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章存档