应用处于非活动状态时iOS处理动态链接

时间:2017-02-07 04:19:18

标签: ios swift firebase-dynamic-links

我的ApplicationDelegate:

let customURLScheme = "dlscheme"

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

FIROptions.default().deepLinkURLScheme = self.customURLScheme
FIRApp.configure()

return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    return application(app, open: url, sourceApplication: nil, annotation: [:])
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
    if let dynamicLink = dynamicLink {
        let message = generateDynamicLinkMessage(dynamicLink)
        if #available(iOS 8.0, *) {
            showDeepLinkAlertView(withMessage: message)
        }
        return true
    }
    if #available(iOS 8.0, *) {
        showDeepLinkAlertView(withMessage: "openURL:\n\(url)")
    } else {
    }
    return false
}
@available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    guard let dynamicLinks = FIRDynamicLinks.dynamicLinks() else {
        return false
    }
    let handled = dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
        let message = self.generateDynamicLinkMessage(dynamiclink!)
        self.showDeepLinkAlertView(withMessage: message)
    }
    return handled
}

func generateDynamicLinkMessage(_ dynamicLink: FIRDynamicLink) -> String {
    let matchConfidence: String
    if dynamicLink.matchConfidence == .weak {
        matchConfidence = "Weak"
    } else {
        matchConfidence = "Strong"
    }
    let message = "App URL: \(dynamicLink.url)\nMatch Confidence: \(matchConfidence)\n"
    return message
}

@available(iOS 8.0, *)
func showDeepLinkAlertView(withMessage message: String) {
    let okAction = UIAlertAction.init(title: "OK", style: .default) { (action) -> Void in
        print("OK")
    }

    let alertController = UIAlertController.init(title: "Deep-link Data", message: message, preferredStyle: .alert)
    alertController.addAction(okAction)
    self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}

这是我的代码。我正在使用firebase动态链接,并像本教程一样实现它:

https://firebase.google.com/docs/dynamic-links/ios

这个样本:

https://github.com/firebase/quickstart-ios/blob/master/dynamiclinks/DynamicLinksExampleSwift/AppDelegate.swift

当我的应用处于后台时,它运行良好。当我点击动态链接时,打开应用程序并使用网址显示提醒。

但是在我的应用程序处于非活动状态(未运行)的情况下,它无效。只需打开应用程序,什么也不做。

我的问题是:当应用程序处于非活动状态时,如何处理动态链接?  抱歉我的英文

3 个答案:

答案 0 :(得分:1)

快捷键5

您可以通过以下方式处理它:

AppDelegate.swift

import Firebase
import FirebaseDynamicLinks

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

    // ... your other code here

    FirebaseApp.configure()

    let activityKey = NSString(string: "UIApplicationLaunchOptionsUserActivityKey")
    if let userActivityDict = launchOptions?[.userActivityDictionary] as? [NSObject : AnyObject], let userActivity = userActivityDict[activityKey] as? NSUserActivity, let webPageUrl = userActivity.webpageURL {
        
        DynamicLinks.dynamicLinks().handleUniversalLink(webPageUrl) { (dynamiclink, error) in
            
            // do some stuff with dynamiclink
            
        }
        
    }
    
    return true
}

答案 1 :(得分:0)

我这样做......

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

 if let option = launchOptions
    {
        if option.keys.contains(UIApplicationLaunchOptionsKey.userActivityDictionary)
        {
            if let userActivityDict = option[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable:Any]
            {
                if userActivityDict.keys.contains(UIApplicationLaunchOptionsKey.userActivityType)
                {
                    if let userActivity = userActivityDict["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {

                        if let webpageURL = userActivity.webpageURL
                        {
                            // Write your code here.
                        }
                    }
                }
            }
        }
    }
}

答案 2 :(得分:0)

在后台运行应用程序时未调用application(_:open:options:)时遇到问题。 Firebase SDK使方法陷入混乱,这就是原因。

我通过在FirebaseAppDelegateProxyEnabled = NO中设置Info.plist来解决了这个问题

有关其工作原理和影响的详细信息,请点击此处https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in