处理Facebook iOS SDK V4.1的App Links的冷启动

时间:2015-05-14 11:10:36

标签: ios facebook applinks

这是在AppDelegate中实现的

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

我在App Link上有一些在Facebook上分享的参数。如果我的应用程序最小化,点击Facebook上的链接打开我的应用程序并调用功能

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

喜欢它应该发生。

问题如果我的应用程序被终止(未最小化)点击Facebook上的链接打开我的应用程序但功能未被调用,因此我无法处理输入参数。

我已经找到了一些关于在Facebook文档中处理冷启动的内容,但该信息已过时。

我是否遗漏了要实施的内容或Facebook漏洞?

1 个答案:

答案 0 :(得分:3)

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.