【发布时间】:2020-01-08 22:45:40
【问题描述】:
让我的自定义 URL 在我的 iOS 应用中正常工作时遇到问题。
当我在 Safari 中打开此 url bdat://hello 时,它会转换回我的应用程序,但不会调用 AppDelegate 方法。
这是方法和相关的Info.plist 数据。我将其分解为这个简单的案例,因为我在 Spotify API 上看到了同样的问题。
我还缺少其他设置吗?
// AppDelegate.swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print(url)
return true
}
// Info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.bdat.test</string>
<key>CFBundleURLSchemes</key>
<array>
<string>bdat</string>
</array>
</dict>
</array>
【问题讨论】:
-
您确定您的应用正在运行吗?也许
application(_:didFinishLaunchingWithOptions:)会被调用。 -
...如果它正在运行,请检查应用程序是否已连接到 Xcode。您需要更改应用程序的启动方式(不是自动启动,而是在 url 触发时),然后点击运行,然后打开 url。
-
"如果您的实现从 application(:willFinishLaunchingWithOptions:) 和 application(:didFinishLaunchingWithOptions:) 方法返回 false,则不会调用此方法。"
-
感谢您的建议。应用程序启动时会调用 didFinishLaunchingWithOptions 并且我返回 true。我将方案更改为“等待启动可执行文件”。我仍然看到相同的行为。如果我理解正确,CFBundleURLName 并没有太大区别,但它与我的捆绑 ID 相同。
-
@Hethcox 我检查了您的 plist 代码和设置,一切正常(
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool方法有效)。很奇怪。