简体   繁体   中英

Handling cold start for App Links for Facebook iOS SDK V4.1

This is implemented in AppDelegate

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

I have App Link with some parameters that is shared on Facebook. If my app is minimized, clicking link on Facebook opens my app and calling function

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool

like it should happened.

Problem is if my app is terminated (not minimized) clicking link on Facebook opens my app but function is not called so I can't process input params.

I have found something about handling cold start in Facebook documentation but that info is outdated.

Did I miss something to implement or it is Facebook bug?

I was boggling my head about this for a while...

When you open an app link and the app is not currently running, the app launches and first calls application:didFinishLaunchingWithOptions . If you return true in this method, then it will follow by calling application:openURL:sourceApplication:annotation: and passing in the proper parameters. However, if application:didFinishLaunchingWithOptions returns false , then application:openURL:sourceApplication:annotation: is never called.

If you are using the FBSDK, then you will be calling

return FBSDKApplicationDelegate.sharedInstance().application(application,
didFinishLaunchingWithOptions: launchOptions)

at the end of your application:didFinishLaunchingWithOptions . If you look at their documentation, it states that it returns

YES if the url was intended for the Facebook SDK, NO if not.

So, because YOUR applink is not intended for the FBSDK, it is returning false . However, the keys UIApplicationLaunchOptionsURLKey and UIApplicationLaunchOptionsSourceApplicationKey ARE available in your launchOptions . In your application:didFinishLaunchingWithOptions , if you call

if let url = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL,
  sourceApplication = launchOptions?[UIApplicationLaunchOptionsSourceApplicationKey] as? String {      
   self.application(application, openURL: url, sourceApplication: sourceApplication, annotation: nil) 
}

then you can get around it. I'm not sure if you're supposed to be calling the AppDelegate methods manually, but it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM