2014-02-13 7 views
12

Я пытаюсь открыть приложение для приложений с яблоками и потянуть адрес. Я попытался это:Открытые карты с конкретным адресом iOS 7

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

Итак, всякий раз, когда это пространство, оно не работает. Как я могу открыть этот адрес?

+1

дать документацию вихрем https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html –

+0

Спасибо, человек, даже не знал, что было ... Я проверю это – TekShock

+0

Я иду: NSString * addressString = [NSString stringWithFormat: @ "http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA"]; – TekShock

ответ

26

Вам необходимо правильно избежать пробелов в URL:

NSString *addressString = @"http://maps.apple.com/?q=1%20Infinite%20Loop,%20Cupertino,%20CA"; 

Edit - это кажется использованием + вместо %20 для пространств решает эту проблему.

Так, чтобы заставить его работать правильно, вы должны использовать это:

NSString *addressString = @"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA"; 
+0

По-прежнему действует, как кнопка ничего не делает. Тем не менее, я получаю сообщение об ошибке: Invalid conversion specifier 'I'. Я знаю, что это значит, но как у исправить? – TekShock

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

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

Вот как я делаю это в 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){ 

           }]; 
    } 
} 

ДЛЯ 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> 

1x

2x

3x

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