openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序。这是Iphone开发中我经常需要用到的一段代码,它仅仅只有一行而已。 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]]; 这个程序通过基础的协议支持拨打电话的功能 译者附: -(IBAction)openMaps {//打开地图 // Where is Apple on the map anyway? NSString* addressText = @”1 Infinite Loop, Cupertino, CA 95014″; addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]; NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]]; }
//打开地图直接搜索中文位置需要特殊编码,例子如下:
- (IBAction)gotoHospitalMapAction: (id)sednder {
NSLog(@"gotoHospitalMapAction"); NSString *strHospitalAddress = @"南京市水西门大街文体西路9号"; NSString *strHospitalMap = @"http://maps.google.com/maps?q="; NSURL *searchMap = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", strHospitalMap, [strHospitalAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; [[UIApplication sharedApplication] openURL:searchMap]; }
-(IBAction)openEmail {//打开mail // Fire off an email to apple support [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]]; } -(IBAction)openPhone {//拨打电话 // Call Google 411 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]]; } -(IBAction)openSms {//打开短信 // Text to Google SMS [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]]; } -(IBAction)openBrowser {//打开浏览器 // Lanuch any iPhone developers fav site [[UIApplication sharedApplication] openURL[NSURL :URLWithString:@"http://itunesconnect.apple.com"]]; }
iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
2012年02月23日
⁄ 编程整理
⁄ 被围观 619+
在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://admin@eyecm.com"
]
] ;
拨号(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
] ;