让我从提供代码开始:
func openDirections(originLat: Double, originLong: Double, destinationLat: Double, destinationLong: Double) {
guard let testURL = URL(string:"comgooglemaps-x-callback://") else { return }
if UIApplication.shared.canOpenURL(testURL) {
let directionsRequest = "comgooglemaps://?saddr=\(originLat),\(originLong)&daddr=\(destinationLat),\(destinationLong)&directionsmode=walking"
guard let directionsURL = URL(string: directionsRequest) else { return }
UIApplication.shared.open(directionsURL, options: [:], completionHandler: nil)
} else {
let alert = UIAlertController(title: "Please install Google Maps in order to use this feature.", message: "", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
self.dismiss(animated: true)
}
alert.addAction(okAction)
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
}
如果我注释掉canOpenURL函数,它将按预期方式成功打开google地图。当我执行检查时,即使它已经安装,它也会返回false并指示用户安装google地图。我尝试插入有效的DirectionsURL和相同的问题。此功能是否不应该执行该操作,还是需要尝试另一个测试URL?该文档使用了我在其示例中尝试过的测试网址。如何检查用户是否有Google Maps并可以打开DirectionsURL?