J'essaie de faire en sorte que mon application ouvre l'application Apple Maps et que l'adresse soit retirée. J'ai essayé ceci:
- (IBAction)openInMaps:(id)sender { NSSsortingng *addressSsortingng = @"http://maps.apple.com/?q=1 Infinite Loop, Cupertino, CA"; NSURL *url = [NSURL URLWithSsortingng:addressSsortingng]; [[UIApplication sharedApplication] openURL:url]; }
et ça :
- (IBAction)openInMaps:(id)sender { NSSsortingng *addressSsortingng = @"http://maps.apple.com/?q=1_Infinite_Loop,_Cupertino,_CA"; NSURL *url = [NSURL URLWithSsortingng:addressSsortingng]; [[UIApplication sharedApplication] openURL:url]; }
Mais le button agit comme si c'était accroché à rien. Mais cela fonctionne:
- (IBAction)openInMaps:(id)sender { NSSsortingng *addressSsortingng = @"http://maps.apple.com/?q=Cupertino,CA"; NSURL *url = [NSURL URLWithSsortingng:addressSsortingng]; [[UIApplication sharedApplication] openURL:url]; }
Donc, chaque fois que c'est un espace, ça ne marche pas. Comment puis-je ouvrir cette adresse?
Vous devez correctement échapper les espaces dans l'URL:
NSSsortingng *addressSsortingng = @"http://maps.apple.com/?q=1%20Infinite%20Loop,%20Cupertino,%20CA";
Edit – il semble utiliser +
au lieu de %20
pour les espaces résout le problème.
Donc, pour le faire fonctionner correctement, vous devez utiliser ceci:
NSSsortingng *addressSsortingng = @"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA";
La version 2 de Swift est bien formatée, gère en toute security les options et ne plante pas votre application:
let baseUrl: Ssortingng = "http://maps.apple.com/?q=" let encodedName = "address".ssortingngByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) ?? "" let finalUrl = baseUrl + encodedName if let url = NSURL(ssortingng: finalUrl) { UIApplication.sharedApplication().openURL(url) }
Swift 3 version:
let baseUrl: Ssortingng = "http://maps.apple.com/?q=" let encodedName = "yourAddress".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! let finalUrl = baseUrl + encodedName if let url = URL(ssortingng: finalUrl) { UIApplication.shared.open(url, options: [:], completionHandler: nil) }
C'est comme ça que je le fais en Objective C … J'essaie toujours la Waze Map d'abord, parce que pour être honnête, c'est une sauce géniale, j'ai ajouté les permissions de file PLIST à la fin:
- (IBAction)getDirectionsAction:(id)sender { NSURL *googleURL = [[NSURL alloc] initWithSsortingng:[NSSsortingng ssortingngWithFormat:@"comgooglemaps://?daddr=%@", @"44.294349,-70.326973"]]; NSURL *googleWebURL = [[NSURL alloc] initWithSsortingng:[NSSsortingng ssortingngWithFormat:@"http://www.maps.google.com/maps?daddr=%@", @"44.294349,-70.326973"]]; NSURL *appleURL = [NSURL URLWithSsortingng:@"http://maps.apple.com/?daddr=311+East+Buckfield+Road+Buckfield+Maine"]; NSURL *wazeURL = [NSURL URLWithSsortingng:@"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){ }]; } }
POUR LE FICHIER 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> <ssortingng>waze</ssortingng> <ssortingng>comgooglemaps</ssortingng> </array> <key>NSLocationAlwaysUsageDescription</key> <ssortingng>For Use for directions</ssortingng>
Swift 2 let baseUrl : Ssortingng = "http://maps.google.com/?q=" let name : Ssortingng = tableCellData[indexPath.row] let encodedName = "address of yours" name.ssortingngByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) let finalUrl = baseUrl + encodedName! let url = NSURL(ssortingng: finalUrl)! UIApplication.sharedApplication().openURL(url)