Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当应用程序处于非活动状态时,iOS处理动态链接_Ios_Swift_Firebase Dynamic Links - Fatal编程技术网

当应用程序处于非活动状态时,iOS处理动态链接

当应用程序处于非活动状态时,iOS处理动态链接,ios,swift,firebase-dynamic-links,Ios,Swift,Firebase Dynamic Links,我的申请者: let customURLScheme = "dlscheme" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { FIROptions.default().deepLinkURLScheme = self.customURLScheme FI

我的申请者:

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 dynamic link,并像本教程一样实现它:

这个样本:

当我的应用程序在后台时,它工作得很好。打开应用程序并在我点击动态链接时显示带有url的警报

但如果我的应用程序处于非活动状态(未运行),它将无法工作。只需打开应用程序,什么也不做

我的问题是:当应用程序处于非活动状态时,如何处理动态链接?
对不起,我的英语不好,我不得不这样做

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.
                        }
                    }
                }
            }
        }
    }
}

当应用程序在后台时,没有调用
应用程序(\uu:open:options:)
时,我遇到了一个问题。Firebase SDK使用swizzle方法,这就是原因

我通过在
Info.plist


有关其工作原理及其影响的更多详细信息,请阅读此处

Swift 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
}

您成功地使它工作了吗?您的6级(!!)条件检查可以用单个级别
if
guard
表示。17行变成5行。
//用dynamiclink做些什么?@TomSawyer取决于你的需要。以我的方式:
如果让url=dynamiclink?.url,让contenttype=url[“contenttype”],让contentid=url[“contentid”]{…}
我不使用Firebase,但从
调用openURL无法使用选项