IOS打电话、发短信、发邮件总结

时间:2022-11-10 14:15:28

一.打电话
打电话有3种实现方式
(1)
这种方式从APP中打电话后,会停留在通信录界面,要手动才能回到App界面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://%@", phoneNum]];  // phoneNum电话号码


(2)
这种方式打完电话后会返回app
 但是:telprompt协议属于苹果的私有协议,一旦程序中使用了此协议,程序无法上架, 针对越狱的机器开发的系统,可以使用此协议

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://%@", phoneNum]];


(3)一般用这种方式打完电话后会返回app

if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
    }
    NSURL *url = [NSURL URLWithString:@"tel://10010"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
}


二.发短信
有2种方式
(1)弹出发短信到phoneNum界面 ,无法指定短信内容,发完短信后不返回app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://%@",phoneNum]];


(2)
如果自定义短信,可以做如下操作:
加入:MessageUI.framework
 
#import <MessageUI/MFMessageComposeViewController.h>
 
实现代理:MFMessageComposeViewControllerDelegate
优点 
 1. 从应用出去能回来
 2. 可以多人
 3. 可以自定义消息,消息支持HTML格式的
而且如果在苹果系统中,如果彼此的手机都是iOS设备,并且开通了iMessage功能,彼此之间的短信

// bodyofmessage:信息内容  recipients:电话号码数组(可以发短信给10个人(数组中的号码))
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
 
    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
 
    if([MFMessageComposeViewController canSendText]) {
        controller.body = bodyOfMessage;   
        controller.recipients = recipients;
        controller.messageComposeDelegate = self;
      [self presentViewController:controller animated:YES completion:nil];
 } }
 // 处理发送完的响应结果
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ 
       if (result == MessageComposeResultCancelled) NSLog(@"Message cancelled"); 
      else if (result == MessageComposeResultSent) NSLog(@"Message sent"); 
      else NSLog(@"Message failed");
      [self dismissViewControllerAnimated:YES completion:nil];


三.发送邮件

发送邮件的为:
导入#import <MessageUI/MFMailComposeViewController.h>
实现代理:MFMailComposeViewControllerDelegate

三.发送邮件
发送邮件的为:
导入#import <MessageUI/MFMailComposeViewController.h>
实现代理:MFMailComposeViewControllerDelegate
 
//发送邮件
-(void)sendMail:(NSString *)subject content:(NSString *)content{
 
    MFMailComposeViewController *controller = [[[MFMailComposeViewController alloc] init] autorelease];
 
    if([MFMailComposeViewController canSendMail])
 
    {
 
        [controller setSubject:subject];
 
        [controller setMessageBody:content isHTML:NO];
 
        controller.mailComposeDelegate = self;
 
        [self presentModalViewController:controller animated:YES];
 
    }    
}
 
//邮件完成处理
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
 
    [self dismissModalViewControllerAnimated:YES];
 
    if (result == MessageComposeResultCancelled)
        NSLog(@"Message cancelled");
    else if (result == MessageComposeResultSent)
        NSLog(@"Message sent"); 
    else 
        NSLog(@"Message failed");  
 
}