打开具有特定地址的地图iOS 7

打开具有特定地址的地图iOS 7,ios,iphone,map,street-address,apple-maps,Ios,Iphone,Map,Street Address,Apple Maps,我正在尝试让我的应用程序打开apple maps应用程序,并将地址调出。我试过这个: - (IBAction)openInMaps:(id)sender { NSString *addressString = @"http://maps.apple.com/?q=1 Infinite Loop, Cupertino, CA"; NSURL *url = [NSURL URLWithString:addressString]; [[UIApplication sharedA

我正在尝试让我的应用程序打开apple maps应用程序,并将地址调出。我试过这个:

- (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];
}

所以,无论何时,只要他们是一个空间,它就不起作用。如何打开此地址?

您需要正确转义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";

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)
}

我在Objective C中就是这样做的……我总是先尝试Waze映射,因为老实说,它是很棒的酱汁,我在最后添加了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){

                                 }];
    }
}
用于PLIST文件

<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>
NSAppTransportSecurity
NSAllowsArbitraryLoads
NSExceptionDomains
http://maps.apple.com
N异常低安全Http负载
NSExceptionRequiresForwardSecretary
n包括多个域
http://maps.google.com/
n包括多个域
N异常低安全Http负载
NSExceptionRequiresForwardSecretary
LSApplicationQueriesSchemes

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)
    }

给文档一个旋转谢谢,伙计,甚至不知道那里有。。。我会检查它我去:NSString*addressString=[NSString stringWithFormat:@“不要不必要地使用
stringWithFormat:
。直接分配字符串。仍然像按钮什么都不做一样。但是,我得到了错误:无效的转换说明符“I”。我知道它的意思,但如何修复?
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)
    }