swiftapple-maps

How to open Apple Maps directly from app?


Im trying to open apple maps directly from app with this code

let url =  "http://maps.apple.com/maps?saddr=&daddr=\(latString),\(longString)"             
UIApplication.shared.open(URL(string: "\(url)")!,options: [:],
completionHandler: nil)

But it opens 'SAFARI' first, then moves to apple maps.The problem is that, from maps i cant go back to my app. The top back button only goes back to safari.

Is there a way to open apple maps directly? Or do i have to use google maps for that?

Edit : The answer is correct but here is the better one

let mapsURL = URL(string: "maps://?saddr=\(latStringS),\(longStringS)&daddr=\(latStringD),\(longStringD)")!

this will show direction to destination from starting location

Solution

  • General Solution

    You should pass app's deeplink as the URL to open them directly. For Maps this is the URL you are looking for:

    let mapsURL = URL(string: "maps://q?\(latString),\(longString)")!
    

    Specific Function

    Also, there is a Specific function for Maps and that is:

    class func openMaps(with mapItems: [MKMapItem], launchOptions: [String : Any]? = nil) -> Bool
    

    You can find out more about this in the documentation


    Specific Framework

    Also, You can use my framework for this that. supports AppleMaps, GoogleMaps, Waze and Maps.Me.

    You can get it from here