貓貓學iOS(四十五)之常用的小功能比如打電話、開啟網址、發郵件、發簡訊開啟其他應用。,ios發郵件

來源:互聯網
上載者:User

貓貓學iOS(四十五)之常用的小功能比如打電話、開啟網址、發郵件、發簡訊開啟其他應用。,ios發郵件

貓貓分享,必須精品

原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

簡介

iOS中的很多小功能都是非常簡單的,幾行代碼就搞定了,比如打電話、開啟網址、發郵件、發簡訊、開啟其他應用等。

打電話方法1

最簡單最直接的方式:直接跳到撥號介面

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

缺點
電話打完後,不會自動回到原應用,直接停留在通話記錄介面

方法2

撥號之前會彈框詢問使用者是否撥號,撥完後能自動回到原應用

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

缺點
因為是私人API,所以可能不會被審核通過(別以為能做壞事了,也就自己爽爽自己)

方法3

建立一個UIWebView來載入URL,撥完後能自動回到原應用

if (_webView == nil) {    _webView = [[UIWebView alloc] initWithFrame:CGRectZero];}[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];

需要注意的是:這個webView千萬不要添加到介面上來,不然會擋住其他介面

發簡訊方法1

直接跳到發簡訊介面,但是不能指定簡訊內容,而且不能自動回到原應用

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

方法2

如果想指定簡訊內容,那就得使用MessageUI架構
包含主標頭檔

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(@"發送失敗");    }}
發郵件方法1

用內建的郵件用戶端,發完郵件後不會自動回到原應用

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

方法2

跟發簡訊的第2種方法差不多,只不過控制器類名叫做:MFMailComposeViewController
假設發送的郵件內容如右圖所示,代碼實現看備忘

郵件發送後的代理方法回調,發完後會自動回到原應用

- (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];
應用間跳轉

有時候,需要在本應用中開啟其他應用,比如從A應用中跳轉到B應用

首先,B應用得有自己的URL地址(在Info.plist中配置)


B應用的URL地址就是:mm://www.nycat.com

接著在A應用中使用UIApplication完成跳轉
NSURL *url = [NSURL URLWithString:@”mm://www.nycat.com”];
[[UIApplication sharedApplication] openURL:url];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.