3-IOS-小功能(打電話、發簡訊、發郵件)

來源:互聯網
上載者:User

標籤:

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>發簡訊1

直接跳到發簡訊介面,但是不能指定簡訊內容,而且不能自動回到原應用NSURL *url = [NSURL URLWithString:@"sms://10010"];[[UIApplication sharedApplication] openURL:url];

2>如果想指定簡訊內容,那就得使用MessageUI架構

 

包含主標頭檔#import <MessageUI/MessageUI.h>在調用發簡訊代碼之前,最好先判斷使用者的裝置能否發簡訊// 不能發簡訊if (![MFMessageComposeViewController canSendText]) return;顯示發簡訊的控制器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://[email protected]"];[[UIApplication sharedApplication] openURL:url];

 

2>跟發簡訊的第2種方法差不多,只不過控制器類名叫做:MFMailComposeViewController

// 不能發郵件if (![MFMailComposeViewController canSendMail]) return;MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];// 設定郵件主題[vc setSubject:@"會議"];// 設定郵件內容[vc setMessageBody:@"今天下午開會吧" isHTML:NO];// 設定收件者清單[vc setToRecipients:@[@"[email protected]"]];// 設定抄送人列表[vc setCcRecipients:@[@"[email protected]"]];// 設定密送人列表[vc setBccRecipients:@[@"[email protected]"]];// 添加附件(一張圖片)UIImage *image = [UIImage imageNamed:@"lufy.jpeg"];NSData *data = UIImageJPEGRepresentation(image, 0.5);[vc addAttachmentData:data mimeType:@"image/jepg" fileName:@"lufy.jpeg"];// 設定代理vc.mailComposeDelegate = self;// 顯示控制器[self presentViewController:vc animated:YES completion:nil];郵件發送後的代理方法回調,發完後會自動回到原應用- (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];

 

3-IOS-小功能(打電話、發簡訊、發郵件)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.