Waze 不从 Swift 加载导航

Waze doesn't load navigation from Swift

我将 Waze 集成到我的 Swift 应用程序中,但是当我单击该按钮时,Waze 打开但导航没有任何反应。我只是看到了应用程序,仅此而已,而不是启动导航。

代码如下:

@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")!)
    }
}

print(urlStr) returns 右边 URL: waze://ul?ll=48.792914,2.366290&navigate=yes,但 Waze 应用程序没有任何反应。

(我将 LSApplicationQueriesSchemes 放在 Info.plist 文件中。)

这里有什么问题?

我解决了这个问题。 Waze documentation 给出了错误的信息,因为他们的 iOS 示例没有按应有的方式打开 Waze 应用程序。它会在移动设备上打开 Safari,然后我们需要单击 link 打开 Waze。

正确的link是:

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

我需要删除 URL 中的 ul


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"]];
  }
}

所选答案对我不起作用,我是 运行 我的设备上安装了 waze 的应用程序,它总是打开应用程序商店。使用此代码,如果已安装,它会打开 waze,如果没有,它会打开应用程序商店。

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")!)
        }
    }