自定义 URL 方案错误:此应用不允许查询方案
Posted
技术标签:
【中文标题】自定义 URL 方案错误:此应用不允许查询方案【英文标题】:Custom URL Scheme Error : This app is not allowed to query for scheme 【发布时间】:2019-10-22 10:34:46 【问题描述】:我有 2 个应用程序,一个客户端和一个服务器。我正在尝试使用 URL 方案在 ios 应用程序之间进行应用程序间通信
(我已经参考了这个https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app作为指导)
这是客户端的plist
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>com.test.serverendapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>serverendapp</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.test.serverendapp</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
iOS端代码
@IBAction func tapToClient()
let urlsrting = "com.test.serverEndApp://requestInfo?userID='22'"
let url = URL(string: urlsrting)!
if UIApplication.shared.canOpenURL(url)
if #available(iOS 10.0, *)
UIApplication.shared.open(url, options: [:]) (success) in
print(" this is \(success)")
else
UIApplication.shared.openURL(url)
else
let alertController = UIAlertController(title: "Error", message:
"There is no such server app here", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
self.present(alertController, animated: true, completion: nil)
服务器端
列表
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>com.test.clientendapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>clientendapp</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.test.clientendapp</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
代码
@IBAction func tapToServer()
let urlsrting = "com.test.clientEndApp://provideInfo?username='Debanjan'"
let url = URL(string: urlsrting)!
if UIApplication.shared.canOpenURL(url)
if #available(iOS 10.0, *)
UIApplication.shared.open(url, options: [:]) (success) in
print(" this is \(success)")
else
UIApplication.shared.openURL(url)
else
let alertController = UIAlertController(title: "Error", message:
"There is no such client app here", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
self.present(alertController, animated: true, completion: nil)
我从他们那里得到的只是
-canOpenURL:URL 失败:“com.test.clientEndApp://provideInfo?username='Debanjan'” - 错误: "此应用不允许查询scheme com.test.clientendapp"
-canOpenURL:URL 失败:“com.test.serverEndApp://requestInfo?userID='22'” - 错误:“此应用 不允许查询方案 com.test.serverendapp"
我已按照所有步骤进行操作,但仍处于这种状态。帮助我意识到我做错了什么
我在 iPhone 8 iOS 12.2 模拟器上运行它,在 xcode 12.2 上
【问题讨论】:
在设备上运行,而不是模拟器。 【参考方案1】:如果你注册,你就是在做相反的事情:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.test.clientEndApp</string>
</array>
你应该调用这个方案
let urlsrting = "com.test.clientEndApp://provideInfo?username='Debanjan'"
您在LSApplicationQueriesSchemes
中缺少com.test.
【讨论】:
我就是这么做的。它仍然给我错误。请检查更新的 plist。 你做了什么?您仍然调用与您注册的方案不同的 url 之间存在差异:com.test.clientEndApp
和 clientEndApp
我更改了您提到的客户端和服务器应用程序的 url 方案。客户端应用现在有 serverendapp 方案,服务器有 clientendapp 方案
是的,即使加了com.test前缀,还是不行。我从你的观点理解,客户端应用程序应该指向 serverapp 的 url 方案,反之亦然。我这样做并运行了该项目。也清理了我的模拟器
看起来您没有尝试过,而且错误完全一样,您 100% 确定方案在各处设置的完全相同?【参考方案2】:
所以,感谢@Lu_ 指出我的错误
积分: 1.客户端应用程序将具有服务器应用程序的方案,反之亦然 2. LS 应该有一个完整的名字,没有这样的 com.test 或什么不是
【讨论】:
以上是关于自定义 URL 方案错误:此应用不允许查询方案的主要内容,如果未能解决你的问题,请参考以下文章
从另一个应用程序打开一个应用程序 - “此应用程序不允许查询方案”