iOS 電話 短信 郵件等等

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

1ios

NSURL *url = [NSURL URLWithString:@"tel://10010"];web

[[UIApplication sharedApplication] openURL:url];瀏覽器

缺點app

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

2url

 

撥號以前會彈框詢問用戶是否撥號,撥完後能自動回到原應用代理

NSURL *url = [NSURL URLWithString:@"telprompt://10010"];orm

[[UIApplication sharedApplication] openURL:url];htm

缺點

由於是私有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 <MessageUI/MessageUI.h>

 

顯示發短信的控制器

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

相關文章
相關標籤/搜索