The UIApplication class provides a way for apps to control and coordinate user events with other apps on the device. One of the features is being able to open the Apple Maps app 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 maps.
Opening Apple 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.607833Next, create a URL from a string containing the latitude and longitude. Remember to add the maps:// prefix.
Important: the URL should have an saddr, meaning starting address, and daddr, meaning destination address. Put your latitude and longitude on the daddr.
let url = URL(string: "maps://?saddr=&daddr=\(latitude),\(longitude)")If you want to learn more about the other Apple Maps scheme options, check Apple's official documentation.
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 Apple Maps and sends the destination information to be loaded automatically.
Here is the full example inside a SwiftUI view:
import SwiftUI
struct ContentView: View {
var body: some View {
Button("Open Apple Maps") {
let latitude = 7.065306
let longitude = 125.607833
let url = URL(string: "maps://?saddr=&daddr=\(latitude),\(longitude)")
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
}
}