iOS 应用程序上的通用链接打开应用程序但不调用处理程序

Posted

技术标签:

【中文标题】iOS 应用程序上的通用链接打开应用程序但不调用处理程序【英文标题】:Universal link on iOS app opens app but doesn't call handler 【发布时间】:2021-10-12 10:02:57 【问题描述】:

我有一个带有通用链接的应用。当我在另一个应用程序中点击链接时,它会打开我的应用程序(Safari 除外,它会打开网页,但在顶部显示一个小横幅以在应用程序中打开,点击“打开”会打开我的应用程序)。

我的问题是我的应用程序中没有执行任何代码。

我已经把来自Apple's documentation的示例代码:

func application(_ application: UIApplication,
                 continue userActivity: NSUserActivity,
                 restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool

    // Get URL components from the incoming user activity.
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let incomingURL = userActivity.webpageURL,
        let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else 
        return false
    


    // Check for specific URL components that you need.
    guard let path = components.path,
    let params = components.queryItems else 
        return false
    
    print("path = \(path)")


    if let albumName = params.first(where:  $0.name == "albumname"  )?.value,
        let photoIndex = params.first(where:  $0.name == "index" )?.value 


        print("album = \(albumName)")
        print("photoIndex = \(photoIndex)")
        return true


     else 
        print("Either album name or photo index missing")
        return false
    

我没有场景支持(所以只有 AppDelegate)。

现在我在 AppDelegate 的任何地方都设置了打印和断点(特别是在上面代码的函数中,以及“应用程序 handleOpen url”中。 当我在另一个应用程序中点击一个 URL 时,它会进入我的应用程序,但执行的唯一方法是 applicationDidBecomeActive。

换句话说,我无法从引导用户访问我的应用的 URL 中获取任何信息...知道为什么吗?

【问题讨论】:

【参考方案1】:

这似乎是苹果的错误。

获取方法调用的解决方案是在它之前添加:

@objc(application:continueUserActivity:restorationHandler:)

【讨论】:

以上是关于iOS 应用程序上的通用链接打开应用程序但不调用处理程序的主要内容,如果未能解决你的问题,请参考以下文章

通用链接, continueUserActivity 和 sourceApplication 未被调用

iOS 8 上的通用链接?

iOS:FBSDKShareDialog 打开应用但不允许用户分享链接

Google Play商店无法打开Beta alpha版本的链接

私人网络服务器上的 Apple 通用链接

Branch.io 自定义网址缩短器和通用链接