持续创作,加快成长!这是我参加「日新计划 10 月更文挑战」的第8天,点击查看活动概况

前语

常用小功用:

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

I 拨号

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

1.1 办法一:tel://

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

缺陷:不会主动回到原运用,直接停留在通话记录页面

1.2 办法二:telprompt://

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

(不引荐私有API: telprompt://)

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

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

1.3 办法三:创立一个UIWebView来加载URL

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

//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


- (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 办法一:直接跳到短信界面

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

缺陷:不能指定短信内容,不能主动回到原运用

2.2 办法二:MessageUI结构

modal办法进行控制器间的切换

#import <MessageUI/MessageUI.h>
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 办法一:用自带的邮件客户端

缺陷:发完邮件后不会主动回来原界面

NSURL *url = [NSURL URLWithString:@"mailto://1887405@139.com"];
[[UIApplication sharedApplication] openURL:url];

3.2 办法二: MFMailComposeViewController

运用结构发送邮件

//2)办法二: MFMailComposeViewController
        MFMailComposeViewController *mailVC =[[MFMailComposeViewController alloc]init];
        //设置邮件
        [mailVC setSubject:@"邮件主题:test:---------"];
        //设置邮件内容
        [mailVC setMessageBody:@"邮件内容: test------" isHTML:NO];
        //设置收件人列表
        [mailVC setToRecipients:@[@"zang_kn@icloud.com",@"hang_kn@hisuntech.com"]];
        //设置抄送列表
        [mailVC setCcRecipients:@[@"84924492@qq.com",@"hang_kn@hisuntech.com"]];
        //设置密送列表
        [mailVC setBccRecipients:@[@"94934863@qq.com",@"90977255@qq.com"]];
        //添加附件--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中配置)

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

<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完结跳转
NSURL *url = [NSURL URLWithString:@"kn://www.hisunpay.com"];
[[UIApplication sharedApplication] openURL:url];

B 运用能够在 AppDelegate 中处理A运用回来的信息。

4.2 运用评分

跳转到AppStore,而且展示自己的运用

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评分

    //跳转到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

NSURL *url = [NSURL URLWithString:@”http://www.baidu.com"];
[[UIApplication sharedApplication] openURL:url];
  1. iOS小技术:文件预览、分享、打印、存储到手机blog.csdn.net/z929118967/…