iOS小技能:拨号、发邮件、短信、应用间跳转

语言: CN / TW / HK

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第8天,点击查看活动详情

前言

常用小功能: 1. 创建一个UIWebView来加载URL@“tel://10010“,拨完号之后能自动回到原界面。 2. 打开网址 3. 发邮件 4. 短信 5. 应用间跳转 6. 应用评分

I 拨号

1.1 方法一:tel://

objectivec NSURL *url = [NSURL URLWithString:@"tel://011018979"]; [[UIApplication sharedApplication] openURL:url];

缺点:不会自动回到原应用,直接停留在通话记录页面

1.2 方法二:telprompt://

拨号之前会弹框询问用户是否拨号,拨完号之后能自动回到原应用

(不推荐私有API: telprompt://) objectivec NSURL *url = [NSURL URLWithString:@"telprompt://011018979"]; [[UIApplication sharedApplication] openURL:url];

缺点:因为是私有API,可能会审核不通过

1.3 方法三:创建一个UIWebView来加载URL

创建一个UIWebView来加载URL,拨完号之后能自动回到原界面(推荐)

objectivec //WKWebView 不支持拨号,UIWebView已经废弃。 if (_webView == nil) { _webView = [[UIWebView alloc] initWithFrame:CGRectZero]; } [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];

注意点:这个UIWebView不要添加到界面,否则会挡住其他界面

1.4 方式四: 创建一个WKWebView来加载URL

创建一个WKWebView来加载URL,拨完号之后能自动回到原界面

问题:WKWebView默认禁止了跳转到appStore, 拨打电话, 唤起邮箱等一系列操作,而这些操作UIWebView是默认支持的。

解决方式: 遵循WKNavigationDelegate代理方法- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler

```objectivec

  • (WKWebView *)webView{

    if(_webView == nil){ _webView = [[WKWebView alloc] initWithFrame:CGRectZero]; } return _webView; } / WKWebView默认禁止了跳转到appStore, 拨打电话, 唤起邮箱等一系列操作,而这些操作UIWebView是默认支持的。 / +(void)telpromptByWebView:(NSString)phone{

    //遵循WKNavigationDelegate代理:- (void)webView:(WKWebView )webView decidePolicyForNavigationAction:(WKNavigationAction )navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler QCTSession.shareQCTSession.webView.navigationDelegate =QCTSession.shareQCTSession;

    //telprompt tel phone = [NSString stringWithFormat:@"tel://%@",phone];//telprompt // -[WKWebView loadRequest:] must be used from main thread only // dispatch_async(dispatch_get_main_queue(), ^{

    [QCTSession.shareQCTSession.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:phone]]];

} / WKWebView默认禁止了跳转到appStore, 拨打电话, 唤起邮箱等一系列操作,而这些操作UIWebView是默认支持的。 / - (void)webView:(WKWebView )webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

NSURL *URL = navigationAction.request.URL;

NSString *scheme = [URL scheme];
UIApplication *app = [UIApplication sharedApplication];
// 打电话
if ([scheme isEqualToString:@"tel"]) {
    if ([app canOpenURL:URL]) {
        [app openURL:URL];
        // 一定要加上这句,否则会打开新页面
        decisionHandler(WKNavigationActionPolicyCancel);


        return;
    }
}

decisionHandler(WKNavigationActionPolicyAllow);

} ```

II 发短信

2.1 方法一:直接跳到短信界面

objectivec NSURL *url = [NSURL URLWithString:@"sms://10010"]; [[UIApplication sharedApplication] openURL:url];

缺点:不能指定短信内容,不能自动回到原应用

2.2 方法二:MessageUI框架

modal方法进行控制器间的切换 ```objectivec

import

MFMessageComposeViewController vc = [[MFMessageComposeViewController alloc] init]; // 设置短信内容 vc.body = @"吃饭了没?"; // 设置收件人列表 vc.recipients = @[@"10010", @"02010010"]; // 设置代理 vc.messageComposeDelegate = 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(@"发送失败");

}

}

```

III 发邮件

3.1 方法一:用自带的邮件客户端

缺点:发完邮件后不会自动返回原界面

objectivec NSURL *url = [NSURL URLWithString:@"mailto://[email protected]"]; [[UIApplication sharedApplication] openURL:url];

3.2 方法二: MFMailComposeViewController

使用框架发送邮件

```objectivec //2)方法二: MFMailComposeViewController

    MFMailComposeViewController *mailVC =[[MFMailComposeViewController alloc]init];

    //设置邮件

    [mailVC setSubject:@"邮件主题:test:---------"];

    //设置邮件内容

    [mailVC setMessageBody:@"邮件内容: test------" isHTML:NO];

    //设置收件人列表

    [mailVC setToRecipients:@[@"[email protected]",@"[email protected]"]];

    //设置抄送列表

    [mailVC setCcRecipients:@[@"[email protected]",@"[email protected]"]];

    //设置密送列表

    [mailVC setBccRecipients:@[@"[email protected]",@"[email protected]"]];

    //添加附件--Adds the specified data as an attachment to the message.

    UIImage *image = [UIImage imageNamed:@"about_logo"];

    NSData *date = UIImagePNGRepresentation(image);//Returns the data for the specified image in PNG format

    /**

     The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be nil.

     */

    [mailVC addAttachmentData:date mimeType:@"image/png" fileName:@"test.png"];

    //设置代理

    [mailVC setMailComposeDelegate:self];

    [self presentViewController:mailVC animated:YES completion:nil];

pragma mark - MFMailComposeViewControllerDelegate 监听didFinishWithResult,进行关闭邮件界面

  • (void)mailComposeController:(MFMailComposeViewController )controller didFinishWithResult:(MFMailComposeResult)result error:(NSError )error{

    /*enum MFMailComposeResult {

    MFMailComposeResultCancelled,
    
    MFMailComposeResultSaved,
    
    MFMailComposeResultSent,
    
    MFMailComposeResultFailed
    

    };*/ [controller dismissViewControllerAnimated:YES completion:^{ switch (result) { case MFMailComposeResultCancelled: NSLog(@"%@",@"发送取消"); break; case MFMailComposeResultFailed: NSLog(@"%@",@"发送失败"); break; case MFMailComposeResultSent: NSLog(@"%@",@"发送成功"); break; case MFMailComposeResultSaved: NSLog(@"%@",@"MFMailComposeResultSaved");//发送取消,并选择了save Draft break; } }]; } ```

IV 应用间跳转

4.1 应用间跳转

  • 首先:B应用有自己的URL地址(Info.plist中配置)

这里写图片描述

xml <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>www.hisunpay.com</string> <key>CFBundleURLSchemes</key> <array> <string>kn</string> </array> </dict> </array>

B应用的URL地址就是:kn://www.hisunpay.com

  • 其次:A应用使用UIApplication完成跳转

objectivec NSURL *url = [NSURL URLWithString:@"kn://www.hisunpay.com"]; [[UIApplication sharedApplication] openURL:url];

B 应用可以在 AppDelegate 中处理A应用返回的信息。

4.2 应用评分

跳转到AppStore,并且展示自己的应用

objectivec NSString *appid= @"444934666"; NSString *str= [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",appid]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

跳转到App Store评分 objectivec //跳转到App Store评分 [scoreItem setOptionBlock:^{ NSString *appId = @"425349261";//网易新闻 NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/app/id%@?mt=8",appId]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }];

V 打开其他常见文件(htm、txt、pdf)

  1. WebView:只需要告诉WebView文件的URL即可
  2. 至于打开一个远程的共享资源,比如http协议的,也可以调用系统自带的Safari浏览器:

打开网址http objectivec NSURL *url = [NSURL URLWithString:@”http://www.baidu.com"]; [[UIApplication sharedApplication] openURL:url];

  1. iOS小技能:文件预览、分享、打印、存储到手机https://blog.csdn.net/z929118967/article/details/125410157?spm=1001.2014.3001.5501