swift - 使用添加的 url 方案时,Urls 不会打开
问题描述
我想用谷歌浏览器打开网址。如果我用 'googlechrome://' 替换 url 前缀,这很好用(ergo google chrome 打开 url)。
但是,我还想检查是否安装了我正在使用“canOpenUrl”方法的chrome,我再次明白我需要确保添加了相应的url方案。到目前为止一切顺利,我在我的 plist 中的 url 方案中添加了“googlechrome”和“googlechromes”,并且“canOpenUrl”方法返回为真。但是没有任何反应,谷歌浏览器根本无法打开。有什么明显的我失踪了吗?
这段代码工作得很好,谷歌浏览器打开了很长时间,我的 plist 中没有任何 url 方案:
if let googleUrl = URL(string: "googlechrome" + "\(urlString.replaceStringPrefix())") {
print("openUrl, googleUrl: ", googleUrl)
UIApplication.shared.open(googleUrl, options: [:])
}
以下为 canOpenUrl 与相应的 plist 条目返回 true,但没有任何反应,Google chrome 不会打开:
<key>CFBundleURLSchemes</key>
<array>
<string>googlechrome</string>
<string>googlechromes</string>
<string>googlechrome-x-callback</string>
</array>
if let googleUrl = URL(string: "googlechrome" + "\(urlString.replaceStringPrefix())") {
print("openUrl, googleUrl: ", googleUrl)
if UIApplication.shared.canOpenURL(googleUrl) {
print("Can open url with Chrome, url: ", googleUrl)
UIApplication.shared.open(googleUrl, options: [:])
} else {
print("Cannot open url with Chrome, falling back to Safari")
if let safariUrl = URL(string: urlString) {
UIApplication.shared.open(safariUrl, options: [:])
}
}
}
解决方案
找到了罪魁祸首。canOpenUrl 将使用上面的 plist 条目返回 true,在各个应用程序中实际打开的正确键是 LSApplicationQueriesSchemes,结果如下:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechrome</string>
<string>googlechromes</string>
<string>googlechrome-x-callback</string>
</array>
推荐阅读
- ruby-on-rails - RabbitMQ 中的常见错误
- php - 如何在 laravel 5.4 中以其他名称保存图像
- php - 如何循环遍历 N 级子子数组 - PHP
- java - 如何在安装页面后只运行一次该页面
- android - 清单合并失败并出现多个错误,生成签名 apk 时查看日志
- python - 如何使用python将一张图像的相位和不同图像的大小组合成一张图像
- javascript - 在 MVC 中调用 AJAX 后,选择框的 JavaScript 文件不起作用
- c++ - cassandra cpp 驱动程序 dev 2.9.0-1 不会安装在 Debian Stretch 上?
- ios - 定制 B2B 和批量购买计划的成本是多少?
- javascript - 为什么我不能使用 Array.prototype.find 和 String.prototype.startsWith 作为过滤器?