情诗网 >情话短信 > 正文

iOS手机打电话、发短信功能

来源:情诗网    2021-03-04    分类:情话短信

一、实现发短信功能

1、发短信需要遵守导入框架并协议

//实现发短信功能需要导入MessageUI框架
#import <MessageUI/MessageUI.h>
//然后遵守MFMessageComposeViewControllerDelegate协议

2、在需要发短信的方法中调用下面代码

//发短信
-(void)SendMessage{
        NSLog(@"发短信");
        
        NSString *message =@"阿敏你好啊,有时间来找我玩";
        NSLog(@"message = %@",message);

        NSString *phone = @"15000000000";
        NSLog(@"phone = %@",phone);
        
//传入要发送到得电话号码,和短信界面预写入短信的内容,调用此方法即可跳到短信发送界面
        [self showMessageView:@[phone] title:self.bankName body:message];
}

3、实现发短信的方法


//发短信
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:@"该设备不支持短信功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}

4、用于判断短信发送结果的方法


//发短信
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
            NSLog(@"信息发送成功");
            
            break;
        case MessageComposeResultFailed:
            //信息传送失败
            NSLog(@"信息传送失败");
            
            break;
        case MessageComposeResultCancelled:
            //信息被用户取消传送
            NSLog(@"信息被用户取消传送");
            
            break;
        default:
            break;
    }
}

二、实现打电话功能

//打电话
-(void)Call{
        NSLog(@"打电话");
        
        NSString *phone = @"15000000000";
        NSLog(@"phone = %@",phone);
        
        UIWebView *webView = [[UIWebView alloc] init];
        NSString *string = [NSString stringWithFormat:@"tel://%@",phone];
        NSURL *url = [NSURL URLWithString:string];
        
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
        
        [self.view addSubview:webView];

}

热门文章