You can use canOpenURL(_ url: URL) -> Bool
method from a UIApplication
instance to have the app try first your preferred option with Google Maps, and if not possible, try Apple Maps. Note that Apple Maps could also not be installed.
Example in Swift 5
// Guard your URL instances.
guard let googleMapsUrl = URL(string: "https://www.google.com/maps/@42.585444,13.007813,6z"),
let appleMapsUrl = URL(string: "http://maps.apple.com/?q=Mexican+Restaurant") else {
print("Error creating URLs")
return
}
// Check if link can be opened with Google Maps.
guard UIApplication.canOpenURL(googleMapsUrl) else {
// Use apple maps.
UIApplication.shared.open(appleMapsUrl, options: [:], completionHandler: nil)
return
}
// Use google maps.
UIApplication.shared.open(googleMapsUrl, options: [:], completionHandler: nil)
Note
The code provided as example in the Google Maps documentation here: https://developers.google.com/maps/documentation/urls/ios-urlscheme is deprecated. You should use:
UIApplication.shared.open("https://www.url,com", options: [:], completionHandler: nil)
Refernece: https://developer.apple.com/documentation/uikit/uiapplication/1622961-openurl