iOS 常用小功能 打电话、打开网址、发邮件、发短信等

一、 打电话

  1. 最简单的 缺点电话打完后,不会返回应用。
    NSURL *url1 = [NSURL URLWithString:@"tel://10086"];
    // 注:该方法已在iOS10弃用
    // [[UIApplication sharedApplication]openURL:url];
    [[UIApplication sharedApplication]openURL:url1 options:@{} completionHandler:nil];

  1. 私有api<慎用> 会弹框询问,拨打完后会返回应用
    NSURL *url2 = [NSURL URLWithString:@"telprompt://10086"];
    [[UIApplication sharedApplication]openURL:url2 options:@{} completionHandler:nil];
  1. 创建一个webView 加载url ,拨打完能自动返回应用
if (_webview == nil){
        // !!不要给webView设置尺寸 会遮挡
        _webview = [[UIWebView alloc]initWithFrame:CGRectZero];
    }
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10086"]]];

二 、发短信

  1. 最简单的 缺点发送后,不会返回应用。
    NSURL *url3 = [NSURL URLWithString:@"sms://10086"];
    [[UIApplication sharedApplication]openURL:url3 options:@{} completionHandler:nil];
  1. 使用MessageUI框架
   //#import <MessageUI/MessageUI.h>

    //显示发短信的控制器
    MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
    //设置短信内容
    vc.body = @"快点给我加点话费。";
    //设置收件人列表
    vc.recipients = @[@"10086", @"1008611"];
    //设置代理
    vc.messageComposeDelegate = (id<MFMessageComposeViewControllerDelegate>)self;
    
    //显示控制器
    [self presentViewController:vc animated:YES completion:nil];
 //代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    if (result == MessageComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MessageComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
}

三、发邮件

  1. 最简单的 缺点发送后,不会返回应用。
    NSURL *url3 = [NSURL URLWithString:@"mailto://[email protected]"];
    [[UIApplication sharedApplication]openURL:url3 options:@{} completionHandler:nil];
  1. 跟短信第二种方法差不多 MFMailComposeViewController
    //判断用户是否已设置邮件账户
    if ([MFMailComposeViewController canSendMail]) { 
         [self sendEmailAction]; // 调用发送邮件的代码
    }else{
         //给出提示,设备未开启邮件服务
    }

    // 创建邮件发送界面
    MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
    // 设置邮件代理
    [mailCompose setMailComposeDelegate:self];
    // 设置收件人
    [mailCompose setToRecipients:@[@"[email protected]"]];
    // 设置抄送人
    [mailCompose setCcRecipients:@[@"[email protected]"]];
    // 设置密送人
    [mailCompose setBccRecipients:@[@"[email protected]"]];
    // 设置邮件主题
    [mailCompose setSubject:@"设置邮件主题"];
    //设置邮件的正文内容
    NSString *emailContent = @"我是邮件内容";
    // 是否为HTML格式
    [mailCompose setMessageBody:emailContent isHTML:NO];
    // 如使用HTML格式,则为以下代码
    // [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES];
    //添加附件
    UIImage *image = [UIImage imageNamed:@"qq"];
    NSData *imageData = UIImagePNGRepresentation(image);
    [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"qq.png"];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"TestPDF" ofType:@"pdf"];
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"TestPDF.pdf"];
    // 弹出邮件发送视图
    [self presentViewController:mailCompose animated:YES completion:nil];
   //代理方法
    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
     {
        关闭邮件界面
       [controller dismissViewControllerAnimated:YES completion:nil];
    
       if (result == MFMailComposeResultCancelled) {
                   NSLog(@"取消发送");
        } else if (result == MFMailComposeResultSent) {
                   NSLog(@"已经发出");
        } else {
                 NSLog(@"发送失败");
        }
     }

四、打开其他常见文件

如果想打开一些常见文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打开

只需要告诉UIWebView文件的URL即可

至于打开一个远程的共享资源,比如http协议的,也可以调用系统自带的Safari浏览器:

NSURL *url = [NSURL URLWithString:@"http://www.baidu.com)"];

[[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];

五、应用评分

为了提高应用的用户体验,经常需要邀请用户对应用进行评分,应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论,如何跳转到AppStore,并且展示自己的应用
方法

NSString *appID = @"725296055”;
NSString *str = [NSString stringWithFormat:
                 @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appID];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

原创文章 18 获赞 102 访问量 7955

猜你喜欢

转载自blog.csdn.net/weixin_46602773/article/details/105006094