The UIApplication class provides a way for apps to control and coordinate user events with the built-in apps on the device. One of the features is being able to open Google Maps through our own app.
More specifically, we will work with LSApplicationQueriesSchemes, a Launch Services key used to provide support for launching apps and matching document types to apps. It utilizes URL types in order to coordinate the execution environment of another app.
Prepare the Info.plist
The first step in setting up Launch Services is registering the URL scheme that we will use in our app. Without this, your app will not know that you are trying to use Launch Services to open another app when checking the URL.
Open your Info.plist file, right-click, and add a new row. Type in LSApplicationQueriesSchemes and change the type to Array.
Select the new row, right-click, and add another row. It will add a new item under LSApplicationQueriesSchemes called Item 0. In the value field for that row, enter comgooglemaps.
Opening Google Maps
When dealing with URL schemes or Launch Services, it is useful to trigger the work from a user event. One common control for this is a SwiftUI Button.
A basic button has this format:
Button(
"Some Text",
action: {
// some code
}
)A button's title text and action can be modified easily, which makes it a good candidate for setting up a quick triggered event.
Inside your action, create two constants that hold the latitude and longitude values. You can change these values anytime, or read them from a text field.
let latitude = 7.065306
let longitude = 125.607833If you want to learn more about other Google Maps scheme options, check Google's official documentation.
Important: the URL should have an saddr, meaning starting address, and daddr, meaning destination address. Put your latitude and longitude on the daddr. This example also uses directionsmode=driving.
let url = URL(string: "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")Next, check whether the URL can be opened by using canOpenURL(_:) from UIApplication.shared. Remember to unwrap your URL.
if UIApplication.shared.canOpenURL(url!) {
// code here
}If the condition passes, the URL is valid and can be loaded.
Inside the if block, open your URL with open(_:options:completionHandler:). Pass the URL, [:] for the options, and nil for the completion handler.
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}After everything runs successfully and the button is triggered, this command opens Google Maps and sends the destination information to be loaded automatically.
Important: if Google Maps is not installed, calling this action will not do anything because it fails the canOpenURL(_:) check. In that case, add an else fallback that opens Google Maps in the browser.
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
let urlBrowser = URL(string: "https://www.google.co.in/maps/dir/??saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")
UIApplication.shared.open(urlBrowser!, options: [:], completionHandler: nil)
}Here is a full example inside a SwiftUI view:
import SwiftUI
struct ContentView: View {
var body: some View {
Button("Open Google Maps") {
let latitude = 7.065306
let longitude = 125.607833
let url = URL(string: "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
let urlBrowser = URL(string: "https://www.google.co.in/maps/dir/??saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")
UIApplication.shared.open(urlBrowser!, options: [:], completionHandler: nil)
}
}
}
}When Google Maps opens, the latitude and longitude values are used as the destination reference. If Google Maps is not installed, the fallback opens the same destination in the browser instead.