我正在尝试让我的应用程序打开Apple地图应用程序并将地址拉出来。我试过这个:
- (IBAction)openInMaps:(id)sender {
NSString *addressString = @"http://maps.apple.com/?q=1 Infinite Loop, Cupertino, CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
和此:
- (IBAction)openInMaps:(id)sender {
NSString *addressString = @"http://maps.apple.com/?q=1_Infinite_Loop,_Cupertino,_CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
但是按钮的作用就像它没有任何东西。但这确实有效:
- (IBAction)openInMaps:(id)sender {
NSString *addressString = @"http://maps.apple.com/?q=Cupertino,CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
所以,只要他们是一个空间就行不通。我该如何打开这个地址?
答案 0 :(得分:26)
您需要正确转义URL中的空格:
NSString *addressString = @"http://maps.apple.com/?q=1%20Infinite%20Loop,%20Cupertino,%20CA";
修改 - 似乎使用+
代替%20
来解决问题。
为了让它正常工作,你必须使用它:
NSString *addressString = @"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA";
答案 1 :(得分:10)
Swift 2版本格式正确,安全地处理选项,并且不会让您的应用崩溃:
let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "address".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) ?? ""
let finalUrl = baseUrl + encodedName
if let url = NSURL(string: finalUrl) {
UIApplication.sharedApplication().openURL(url)
}
答案 2 :(得分:3)
这就是我在Objective C中的表现......我总是首先尝试Waze Map,因为老实说,它很棒,我在最后添加了PLIST文件权限:
- (IBAction)getDirectionsAction:(id)sender {
NSURL *googleURL = [[NSURL alloc]
initWithString:[NSString stringWithFormat:@"comgooglemaps://?daddr=%@", @"44.294349,-70.326973"]];
NSURL *googleWebURL =
[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://www.maps.google.com/maps?daddr=%@",
@"44.294349,-70.326973"]];
NSURL *appleURL = [NSURL URLWithString:@"http://maps.apple.com/?daddr=311+East+Buckfield+Road+Buckfield+Maine"];
NSURL *wazeURL = [NSURL URLWithString:@"waze://?ll=44.294349,-70.326973&navigate=yes"];
// Lets try the Waze app first, cuz we like that one the most
if ([[UIApplication sharedApplication] canOpenURL:wazeURL]) {
[[UIApplication sharedApplication] openURL:wazeURL
options:@{}
completionHandler:^(BOOL success){
}];
return;
}
// Lets try the Apple Maps app second (great success rate)
if ([[UIApplication sharedApplication] canOpenURL:appleURL]) {
[[UIApplication sharedApplication] openURL:appleURL
options:@{}
completionHandler:^(BOOL success){
}];
return;
}
// If those 2 are unsuccessful, let's try the Google Maps app
if ([[UIApplication sharedApplication] canOpenURL:googleURL]) {
[[UIApplication sharedApplication] openURL:googleURL
options:@{}
completionHandler:^(BOOL success){
}];
return;
}
// Uh, oh...Well, then lets launch it from the web then.
else {
[[UIApplication sharedApplication] openURL:googleWebURL
options:@{}
completionHandler:^(BOOL success){
}];
}
}
FOR PLIST FILE
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://maps.apple.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>http://maps.google.com/</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>waze</string>
<string>comgooglemaps</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>For Use for directions</string>
答案 3 :(得分:3)
Swift 3版本:
let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "yourAddress".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let finalUrl = baseUrl + encodedName
if let url = URL(string: finalUrl)
{
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
答案 4 :(得分:0)
Swift 2
let baseUrl : String = "http://maps.google.com/?q="
let name : String = tableCellData[indexPath.row]
let encodedName = "address of yours"
name.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let finalUrl = baseUrl + encodedName!
let url = NSURL(string: finalUrl)!
UIApplication.sharedApplication().openURL(url)