陈斌彬的技术博客

Stay foolish,stay hungry

iOS开发中使用[[UIApplication sharedApplication] openURL:] 加载其它应用

在 iOS 开发中,经常需要调用其它 App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的 url 参数的模式来调用不同的 App。

通过 openURL 方法可以调用如下应用:

调用浏览器(Safari Browser)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]];  

调用谷歌地图(Google Maps)

NSString *addressText = @"7 Hanover Square, New York, NY 10004";  
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];  
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];  
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];  

调用邮件客户端(Apple Mail)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];  

拨号(Phone Number)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://6463777303"]];  

调用短信(SMS)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];  

调用应用商店(AppStore)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8"]];