10

I integrated Waze into my Swift app, but when I click on the button, Waze opens but nothing happens with the navigation. I juste see the app and that's all, instead of launching the navigation.

Here is the code:

@IBAction func openWazeAction(_ sender: Any) {
    // open waze
    if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
        let urlStr = String(format: "waze://ul?ll=%f,%f&navigate=yes", (selectedBorne?.location?.x)!, (selectedBorne?.location?.y)!)

        print(urlStr)

        UIApplication.shared.open(URL(string: urlStr)!)
    } else {
        UIApplication.shared.open(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
    }
}

The print(urlStr) returns the right URL: waze://ul?ll=48.792914,2.366290&navigate=yes, but nothing happens in the Waze app.

(I put the LSApplicationQueriesSchemes in the Info.plist file.)

What is wrong here?

cusmar
  • 1,903
  • 1
  • 20
  • 39
  • Does it work with `let urlString = https://waze.com:/ul?ll=48.792914,2.366290&navigate=yes` Does something happen? Source: https://developers.google.com/waze/deeplinks/ – kuzdu Feb 07 '18 at 11:15
  • It opens a Safari view, I think they made a mistake in their documentation about that, because it is not logic to verify if `waze://` exists, then open `https://` url. – cusmar Feb 07 '18 at 13:45

2 Answers2

15

I fixed the problem. The Waze documentation gives wrong information because their iOS example doesn't open the Waze app as it should be. It opens Safari on mobile and then we need to click on a link to open Waze.

The correct link is:

waze://?ll={latitude},{longitude}&navigate=yes

I needed to remove ul in the URL.


Swift

func navigateTo(latitude: Double, longitude: Double) {
    if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
        // Waze is installed. Launch Waze and start navigation
        let urlStr = String(format: "waze://?ll=%f,%f&navigate=yes", latitude, longitude)
        UIApplication.shared.open(URL(string: urlStr)!)
    } else {
        // Waze is not installed. Launch AppStore to install Waze app
        UIApplication.shared.open(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
    }
}

Objective-C

(void) navigateToLatitude:(double)latitude longitude:(double)longitude
{
  if ([[UIApplication sharedApplication]
    canOpenURL:[NSURL URLWithString:@"waze://"]]) {
      // Waze is installed. Launch Waze and start navigation
      NSString *urlStr =
        [NSString stringWithFormat:@"waze://?ll=%f,%f&navigate=yes",
        latitude, longitude];
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
  } else {
    // Waze is not installed. Launch AppStore to install Waze app
    [[UIApplication sharedApplication] openURL:[NSURL
      URLWithString:@"http://itunes.apple.com/us/app/id323229106"]];
  }
}
cusmar
  • 1,903
  • 1
  • 20
  • 39
  • 1
    That's great, but what if I want to navigate by search term and not long/lat? – Gal Feb 26 '18 at 08:05
  • You should encode your search term (like [here](https://stackoverflow.com/a/39767536/6488573)) and then pass it like that in your URL: `waze://?q=` your search term. – cusmar Feb 26 '18 at 09:06
  • 1
    Trying this `"waze://?q=19+Hatzfira+st,+Jerusalem,+Israel" `, opens Waze but without navigation. – Gal Feb 26 '18 at 09:19
  • You should convert your search term into latitude and longitude using GeoCode – cusmar Feb 26 '18 at 11:19
  • You can try this: `query = 'gas station Israel'` `waze://?q=query` it's work for me, and if if the user doesn't have waze on the device you can open on browser like this: `https://waze.com/ul?q=${query}` – Idan Apr 23 '19 at 12:20
  • if i want to pass source and destination location then how can i pass ? – Nirav Kotecha Mar 15 '22 at 06:43
1

The selected answer was not working for me, i was running the app on my device with waze installed and it always opened the appstore. With this code it opens waze if it is installed and it opens the appstore if not.

let urlStr = String(format: "waze://?ll=%f,%f&navigate=yes", latitude, longitude)
UIApplication.shared.open(URL(string: urlStr)!) { didOpen in
        
        if !didOpen {
            UIApplication.shared.open(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
        }
    }
Mario Jaramillo
  • 567
  • 6
  • 9