iOS開發中使用[[UIApplication sharedApplication] openURL:]加載其它應用
在iOS開發中,經常需要調用其它App,如撥打電話、發送郵件等。UIApplication:openURL:方法是實作這一目的的最簡單方法,該方法一般通過提供的url參數的模式來調用不同的App。
通過openURL方法可以調用如下應用:
- 調用谷歌地圖(Google Maps)
- 調用郵件用戶端(Apple Mail)
- 撥号(Phone Number)
- 調用短信(SMS)
- 調用浏覽器(Safari Browser)
- 調用應用商店(AppStore)
調用谷歌地圖(Google Maps)
URL模式:http : //maps.google.com/maps?q=<strong>${QUERY_STRING}</strong>
代碼示例:
NSString * searchQuery = @ "1 Infinite Loop, Cupertino, CA 95014";
searchQuery = [addressText stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];
NSString * urlString = [ NSString stringWithFormat : @ "http://maps.google.com/maps?q=%@", searchQuery ];
[ [UIApplication sharedApplication ] openURL : [ NSURL URLWithString :urlText ] ];
調用郵件用戶端(Apple Mail)
URL模式:mailto : //<strong>${EMAIL_ADDRESS}</strong>
代碼示例:
[ [UIApplication sharedApplication ] openURL : [ NSURL URLWithString : @ "mailto://[email protected]" ] ];
撥号(Phone Number)
URL模式:tel : //<strong>${PHONE_NUMBER}</strong>
代碼示例:
[ [UIApplication sharedApplication ] openURL : [ NSURL URLWithString : @ "tel://10086" ] ];
調用短信(SMS)
URL模式:sms :<strong>$ {PHONENUMBER_OR_SHORTCODE }< /strong>
代碼示例:
[ [UIApplication sharedApplication ] openURL : [ NSURL URLWithString : @ "sms:10086" ] ];
調用浏覽器(Safari Browser)
代碼示例:
NSURL *url = [ NSURL URLWithString : @ "http://eyecm.com" ];
[ [UIApplication sharedApplication ] openURL :url ];
調用應用商店(AppStore)
URL模式:http : //phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8
代碼示例:
NSURL *appStoreUrl = [ NSURL URLWithString : @ "http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&amp;mt=8" ];
[ [UIApplication sharedApplication ] openURL :appStoreUrl ];