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

 

 

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

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地址就是:mj://ios.itcast.cn

 

接著在A應用中使用UIApplication完成跳轉

NSURL *url = [NSURL URLWithString:@"mj://ios.itcast.cn"];

[[UIApplication sharedApplication] openURL:url];

 

 

應用評分

 

 

為了提高應用的使用者體驗,經常需要邀請使用者對應用進行評分

 

應用評分無非就是跳轉到AppStore展示自己的應用,然後由使用者自己撰寫評論

 

如何跳轉到AppStore,並且展示自己的應用

方法1

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]];

 

方法2

NSString *str = [NSString stringWithFormat:

@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];


 


相關文章

聯繫我們

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