我无法使动态链接或通用链接正常工作。我的应用程序无法读取URL。我从一个新项目开始,将com.company.AppLoginView的URL方案和以下代码添加到AppDelegate中。我在com.company.AppLoginView:// Register?User = Name的注释中添加了一个url。单击链接要求打开应用程序,然后将其打开。但是,似乎没有任何内容传递给应用程序,因此未调用以下内容。我究竟做错了什么?这是一个完全空的应用程序,应该可以运行。
func application(_ app: UIApplication, open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if let scheme = url.scheme,
scheme.localizedCaseInsensitiveCompare("com.myApp") == .orderedSame,
let view = url.host {
var parameters: [String: String] = [:]
URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
parameters[$0.name] = $0.value
}
print("View:\(view) Params:\(parameters) Scheme:\(scheme)")
// redirect(to:view,with:parameters)}返回真}
您应将ULR方案和URL标识符添加到plist
。您可以使用此:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.company.AppLoginView</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.company.AppLoginView</string>
</array>
</dict>
</array>
并使用AppDelegate中的print in方法进行检查
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print(url)
return true
}
打开Safari并输入您的地址
com.company.AppLoginView:// Register?User = Name