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://188[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小技能:檔案預覽、分享、列印、儲存到手機http://blog.csdn.net/z929118967/article/details/125410157?spm=1001.2014.3001.5501