【发布时间】:2018-07-09 11:54:15
【问题描述】:
实现以下目标的正确 URL 格式是什么:
- 使用 Universal Link 从 iOS 上的其他应用打开 Google 地图应用。
- 根据纬度和经度两个坐标设置目的地,并让用户选择运输方式。
什么不起作用:
let encoded = "https://www.google.com/maps/dir/?api=1&destination=-20.021999%2C57.579075"
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
另外,我尝试使用addingPercentEncoding 方法对 URL 进行编码:
let string = "https://www.google.com/maps/dir/?api=1&destination=\(lat),\(long)"
let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
两种情况的结果是一样的: “不支持的链接 - Google 地图无法打开此链接”
另一方面,如果我删除 Google 地图应用程序,或在模拟器中尝试相同的代码,一切正常,并且我达到了我想要的结果:
为什么同一个链接成功启动“Web”应用,却无法被原生应用识别?
有问题的字符串:https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075
我正在关注的指南:Google Maps Guide
【问题讨论】:
-
@A.K.M 该答案不适用于通用链接。
标签: swift google-maps deep-linking urlencode ios-universal-links