swiftswizzlingfirebase-dynamic-linkslocalytics

Firebase Dynamic Link can't find continueUserActivity when Localytics implemented


While implementing Firebase Dynamic link on iOS, there is an error message in the debugging console when you click open dynamic link:

FIRAnalytics/WARNING Implementation of application:continueUserActivity:restorationHandler: not found. Please add the handler into your App Delegate. Class: LLAppDelegateProxy

I create a minimized project to reproduce this issue. The new project only contains

Pod 'Localytics'
Pod 'Firebase/DynamicLinks’

And the only add code to AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    FIRApp.configure()
    Localytics.autoIntegrate("apikey", launchOptions: launchOptions)

    return true
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
    if let dynamicLink = dynamicLink {
        print(dynamicLink.url)
        return true
    }

    return false
}

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
        print(dynamiclink?.url)
    }

    return handled
}

It looks like Firebase tries to call application:continueUserActivity:restorationHandler: in Localytics' LLAppDelegateProxy instead of AppDelegate.swift. There is also a GitHub issue post from Branch.io: https://github.com/BranchMetrics/ios-branch-deep-linking/issues/485

The post states that there is a conflict between Google Analytics and Localytics and causes Branch not to be able to find the function application:continueUserActivity:restorationHandler: in the right place.

I follow their third suggestion to change the method swizzling:

//Their solution in Objc

SwizzleInstanceSelectorWithNewSelector(
    [[UIApplication sharedApplication].delegate class], 
    @selector(application:continueUserActivity:restorationHandler:),
    [self class], 
    @selector(BRapplication:continueUserActivity:restorationHandler:)
);

//My swift version in AppDelegate.swift

let originalSelector = #selector(AppDelegate.application(_:continueUserActivity:restorationHandler:))
let swizzledSelector = #selector(AppDelegate.firApplication(_:continueUserActivity:restorationHandler:))

let originalMethod = class_getClassMethod(AppDelegate.self, originalSelector)
let swizzledMethod = class_getClassMethod(AppDelegate.self, swizzledSelector)

method_exchangeImplementations(originalMethod, swizzledMethod)

However, this doesn't work for me. I'm still seeing the warning and link is still not handled.

Could you help me to fix the method swizzling or you have a better solution :]


Solution

  • Sorry for the late answer about this issue.

    We reached out to both Localytics and Firebase. Localytics offered us a solution which requires switching from auto-integration to manual integration. Although it is a bit annoying to use manual integration but it solves the issue :]