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