iOS- 如何將應用整合發簡訊、發郵件、打電話

來源:互聯網
上載者:User

標籤:style   class   blog   code   ext   color   

1.發簡訊           
實現打電話的功能,主要二種方法,下面就分別說說它們的優缺點。

1.1.發簡訊(1)——URL        
// 直接撥號,撥號完成後會停留在通話記錄中

a、方法:
NSURL *url = [NSURL URLWithString:@"sms://10010"];

[[UIApplication sharedApplication] openURL:url];

b、優點:
–簡單
c、缺點:
–不能指定簡訊內容,而且不能自動回到原應用

1.2發簡訊(2)——MessageUI架構       
如果自訂簡訊,需要使用一個架構MessageUI。

優點

a. 從應用出去能回來

b. 可以多人

c. 可以自訂訊息,訊息支援HTML格式的

 

而且如果在蘋果系統中,如果彼此的手機都是iOS裝置,並且開通了iMessage功能,彼此之間的簡訊是走網路通道,而不走電訊廠商的通道!

 

- (void)msg2{// 判斷使用者裝置能否傳送簡訊if (![MFMessageComposeViewController canSendText]) {return;}

// 1. 執行個體化一個控制器

MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

// 2. 設定簡訊內容   
// 1) 收件者
controller.recipients = @[@"10010", @"10086"];

// 2) 簡訊內容
controller.body = @"給您拜個晚年,祝您晚年快樂!";

// 3) 設定代理
controller.messageComposeDelegate = self;

// 3. 顯示簡訊控制器
[self presentViewController:controller animated:YES completion:nil];
}

 

記得發完簡訊記得調用代理方法關閉視窗


#pragma mark 簡訊控制器代理方法
/**
簡訊發送結果

MessageComposeResultCancelled, 取消
MessageComposeResultSent, 發送
MessageComposeResultFailed 失敗
*/
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
NSLog(@"%d", result);

// 在物件導向程式開發中,有一個原則,誰申請,誰釋放!
// *** 此方法也可以正常工作,因為系統會將關閉訊息發送給self
// [controller dismissViewControllerAnimated:YES completion:nil];

// 應該用這個!!!
[self dismissViewControllerAnimated:YES completion:nil];
}

 

2.發郵件            

- (void)sendmail
{
// 1. 先判斷能否發送郵件
if (![MFMailComposeViewController canSendMail]) {
// 提示使用者佈建郵箱
return;
}

// 2. 執行個體化郵件控制器,準備發送郵件
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];

// 1) 主題 xxx的工作報告
[controller setSubject:@"我的工作報告"];
// 2) 收件者
[controller setToRecipients:@[@"[email protected]"]];

// 3) cc 抄送
// 4) bcc 密送(偷偷地告訴,打個小報告)
// 5) 本文
[controller setMessageBody:@"這是我的<font color=\"blue\">工作報告</font>,請審閱!<BR />P.S. 我的頭像牛X嗎?" isHTML:YES];

// 6) 附件
UIImage *image = [UIImage imageNamed:@"頭像1.png"];
NSData *imageData = UIImagePNGRepresentation(image);
// 1> 附件的位元據
// 2> MIMEType 使用什麼應用程式開啟附件
// 3> 收件者接收時看到的檔案名稱
// 可以添加多個附件
[controller addAttachmentData:imageData mimeType:@"image/png" fileName:@"頭像.png"];

// 7) 設定代理
[controller setMailComposeDelegate:self];

// 顯示控制器
[self presentViewController:controller animated:YES completion:nil];
}

 

同樣要記得發完郵件記得調用代理方法關閉視窗


#pragma mark - 郵件代理方法
/**
MFMailComposeResultCancelled, 取消
MFMailComposeResultSaved, 儲存郵件
MFMailComposeResultSent, 已經發送
MFMailComposeResultFailed 發送失敗
*/
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// 根據不同狀態提示使用者
NSLog(@"%d", result);

[self dismissViewControllerAnimated:YES completion:nil];
}

 

3.打電話           
打電話有三種方式可以實現,優缺點也各不同

3.1.打電話不回引用           

- (void)tel1
{
// 直接撥號,撥號完成後會停留在通話記錄中
NSURL *url = [NSURL URLWithString:@"tel://10010"];

[[UIApplication sharedApplication] openURL:url];
}

3.2.出去打電話然後回來        

- (void)tel2
{
// 但是:telprompt協議屬於蘋果的私人協議,一旦程式中使用了此協議,程式無法上架
// 針對越獄的機器開發的系統,可以使用此協議
NSURL *url = [NSURL URLWithString:@"telprompt://10010"];

[[UIApplication sharedApplication] openURL:url];
}

 

3.3藉助UIWebView打電話(會回來)  
一般都是用這種,解決了不越獄的問題。


- (void)tel3
{
// 提示:不要將webView添加到self.view,如果添加會遮擋原有的視圖
// 懶載入
if (_webView == nil) {
_webView = [[UIWebView alloc] init];
}
NSLog(@"%p", _webView);

// _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
// [self.view addSubview:_webView];

NSURL *url = [NSURL URLWithString:@"tel://10010"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[_webView loadRequest:request];
}

 

相關文章

聯繫我們

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