openURL的使用方法:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appString]];
其中系統的appString有:
1.Map http://maps.google.com/maps?q=Shanghai
2.Email mailto://myname@google.com
3.Tel tel://10086
4.Msg sms://10086
openURL能幫助你運行Maps,SMS,Browser,Phone甚至其他的應用程序。這是iPhone開發中我經常需要用到的一段代碼,它僅僅只有一行而已。
//打開地圖
- (IBAction)openMaps {
NSString*addressText = @"beijing"; //@"1Infinite Loop, Cupertino, CA 95014";
addressText =[addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString*urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
NSLog(@"urlText=============== %@", urlText);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
}
//打開mail
- (IBAction)openEmail {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
}
//撥打電話 - (IBAction)openPhone {
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"tel://8004664411"]];
}
使用這種方式撥打電話時,當用戶結束通話后,iphone界面會停留在電話界面。
用如下方式,可以使得用戶結束通話后自動返回到應用:
UIWebView*callWebview =[[UIWebView alloc] init];
NSURL *telURL =[NSURL URLWithString:@"tel:10086"];// 貌似tel:// 或者 tel: 都行
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
//記得添加到view上
[self.view addSubview:callWebview];
還有一種私有方法:(可能不能通過審核)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]];
//打開短信
- (IBAction)openSms {
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"sms://466453"]];
}
//打開瀏覽器
-(IBAction)openBrowser {
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"http://itunesconnect.apple.com"]];
}