当应用程序不在后台时打开通用链接

ray*_*ray 5 ios swift ios-universal-links swift3

我有与此问题类似的情况iOS:如果应用未打开,苹果通用链接?。当我单击通用链接时,如果该应用程序func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {}不在后台,则该应用程序将无法进入。

我在中添加了一些代码didFinishLaunchingWithOptions。但是,它不起作用。非常感谢任何人的帮助。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let activityDic = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary]
    if activityDic != nil {
        // Continue activity here
        self.window?.rootViewController?.restoreUserActivityState(activityDic as! NSUserActivity)
    }


   return true
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {          
            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "xxx") as? XXXTableViewController {
                if let window = self.window, let rootViewController = window.rootViewController {
                    var currentController = rootViewController
                    while let presentedController = currentController.presentedViewController {
                        currentController = presentedController
                    }
                    currentController.present(controller, animated: true, completion: nil)
                }
            }
    }

    return true

}
Run Code Online (Sandbox Code Playgroud)

Bog*_*ogy 5

将此代码放入didFinishLaunchingWithOptions函数中,以在应用启动时打开URL(Swift 3代码):

    if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL { //Deeplink
        // process url here
    }
    else if let activityDictionary = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] { //Universal link
        for key in activityDictionary.keys {
            if let userActivity = activityDictionary[key] as? NSUserActivity {
                if let url = userActivity.webpageURL {
                    // process url here
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Sve*_*hev 2

您可以访问初始化时传递给应用程序的启动选项字典中的 URL。方法内部的实现示例AppDelegate application(_:didFinishLaunchingWithOptions:)

// Catch if open app from url
if let options = launchOptions {
    for key in options.keys {
        if(key == UIApplicationLaunchOptionsKey.url) {
            if let url = options[key] as? URL {
                AppDelegate.handleOpenURLWhenAppIsNotOpen(url)
            }
        }
    }
}

// or
if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
    AppDelegate.handleOpenURLWhenAppIsNotOpen(url)
}
Run Code Online (Sandbox Code Playgroud)

AppDelegate你需要的方法中:

private class func handleOpenURLWhenAppIsNotOpen(_ url: URL) {
     //can make what you like
}
Run Code Online (Sandbox Code Playgroud)