Skip to content
This repository has been archived by the owner on Nov 2, 2019. It is now read-only.

FBSDKApplicationDelegate in FacebookCore? #35

Closed
1dotd4 opened this issue Aug 3, 2016 · 20 comments
Closed

FBSDKApplicationDelegate in FacebookCore? #35

1dotd4 opened this issue Aug 3, 2016 · 20 comments

Comments

@1dotd4
Copy link

1dotd4 commented Aug 3, 2016

After the login and the permission agreement the web view is blank and the only button is "Done" which close and cancel the login. I followed the developer guide.

see update 1
see last update (2)

Found: Application delegate haven't the correct "openURL" methods..
The guide is incorrect when they don't say you have to write those two rows..
But what is the native library "AppDelegate"?

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

white web screen here

Update 1

The first problem (White screen) was solved by adding the application delegate method (Not mentioned in the guide)
The second problem is that with FBSDKApplicationDelegate works.. but with ApplicationDelegate (or if used into the ios application manager FacebookCore.ApplicationDelegate) the application crash because UIApplicationOpenURLOptionsAnnotation is nil.

Update 2 and recap

The problem of the white web screen after accepting the permission agreement was solved adding the facebook app delegate.
The second problem of the applicationDelegate method has been solved by replacing the FBSDKApplicationDelegate in functions

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

with FacebookCore.ApplicationDelegate (that I hope it will be named FBApplicationDelegate) as it follow

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    return FacebookCore.ApplicationDelegate.shared.application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey] ?? [])
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FacebookCore.ApplicationDelegate.shared.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

Note the ?? []:
DO NOT USE !

Thanks to @nlutsenko 👍

@1dotd4 1dotd4 closed this as completed Aug 3, 2016
@1dotd4 1dotd4 reopened this Aug 3, 2016
@1dotd4 1dotd4 changed the title White Web Screen FBSDKApplicationDelegate in FacebookCore? Aug 3, 2016
@nlutsenko
Copy link
Contributor

nlutsenko commented Aug 3, 2016

The native Swift class to get the same functionality is ApplicationDelegate (without FBSDK prefix).

@1dotd4
Copy link
Author

1dotd4 commented Aug 3, 2016

Gotcha

@1dotd4 1dotd4 closed this as completed Aug 3, 2016
@1dotd4
Copy link
Author

1dotd4 commented Aug 3, 2016

No wait if it is ApplicationDelegate.. how swift recognize the ApplicationDelegate (ios native) and the ApplicationDelegate (FacebookCore) ?

And I've tried and it doesn't work.

@1dotd4 1dotd4 reopened this Aug 3, 2016
@nlutsenko
Copy link
Contributor

Swift is using the type system and inferred module semantics.
Better explained with an example:
In case if you don't have ApplicationDelegate in your Swift module - it will recognize FacebookCore's one and give you an ability to use it.
If you have ApplicationDelegate type in your module - then you want to use FacebookCore.ApplicationDelegate, as that has explicit module reference.

Hope it makes sense now ;)
Looks like we might want to rename the methods there, though as we are likely to class with a generic ApplicationDelegate. Am going to send a patch in a few minutes.

@nlutsenko
Copy link
Contributor

nlutsenko commented Aug 3, 2016

Also, as a hint - the call looks like this:

ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

and

ApplicationDelegate.shared.application(application,
                                       openURL: url,
                                       sourceApplication: sourceApplication,
                                       annotation: annotation)

@1dotd4
Copy link
Author

1dotd4 commented Aug 3, 2016

I tried

return FacebookCore.ApplicationDelegate.shared.application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey]!)

but options[UIApplicationOpenURLOptionsAnnotationKey] is nil...
and when I do this with

return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])

works..

@nlutsenko
Copy link
Contributor

Try the following:

return FacebookCore.ApplicationDelegate.shared.application(app, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey] ?? [])

Going to provide an implementation for a new method in a brief moment, since in case you are target iOS 9.0+ - the old one is deprecated.

@nlutsenko
Copy link
Contributor

Let me know if the above method works for you with Swift SDK.

@1dotd4
Copy link
Author

1dotd4 commented Aug 3, 2016

Yeah it works! 😃
simulator screen shot 03 ago 2016 23 06 22

@RajChanchal
Copy link

RajChanchal commented Nov 1, 2016

This works for me:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }

@AquilaSagitta
Copy link

AquilaSagitta commented Feb 8, 2017

Adding this to appDelegate fixed this issue for me (using fbsdk 4.19.0)

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url as URL!, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }

@borisyurkevich
Copy link

I got deprecated warning from Facebook SDK and this fixed it. 🤔

import FacebookCore
import FacebookLogin

public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    let isFacebookCanOpen = SDKApplicationDelegate.shared.application(app, open: url as URL!, options: options)
    return isFacebookCanOpen
}

Tested on FBSDKCoreKit 4.20.0
FBSDKLoginKit 4.20.0
FacebookCore (0.2.0)
Using FacebookLogin (0.2.0)

@1dotd4
Copy link
Author

1dotd4 commented Apr 4, 2017

@RajChanchal @AquilaSagitta @borisyurkevich
Because now it's updated...

@ghost
Copy link

ghost commented Jun 19, 2017

@nlutsenko Could you clarify the latest AppDelegate functions for iOS 10? Thx.

@RajChanchal
Copy link

@savethedave following works fine in iOS 10:

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

@ghost
Copy link

ghost commented Jun 20, 2017

Thx @RajChanchal you are correct.

FWIW, I was following all guides and forum posts to the letter, but still having trouble. Then I reinstalled the SDK using Cocoapods instead of manual compiling, and it all worked fine. To be fair to FB, they do recommend using Cocoapods clearly in their docs, but other parts of the docs are obviously lacking, and probably wasting thousands of developer hours monthly - an unfortunate exception to the generally good docs FB has had in recent years.

For reference to others having trouble, here's the relevant parts of my AppDelegate file that are working for me as of this date (iOS 10, SDK 0.2.0):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    let appId = SDKSettings.appId
    if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host == "authorize" { // Facebook
        return SDKApplicationDelegate.shared.application(app, open: url, options: options)
    }
    return false
}

@metisdev
Copy link

When

FacebookCore.SDKApplicationDelegate.shared.application(app, didFinishLaunchingWithOptions: launchOptions)

is called, do we also need to call :

FacebookCore.AppEventsLogger.activate()

?

@1dotd4
Copy link
Author

1dotd4 commented Jun 24, 2018

Should I remember y'all that the issue is closed and it's related to an older version?

@hk0i
Copy link

hk0i commented Jun 18, 2019

I'm still getting this issue with FacebookCore 0.7.0

  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if #available(iOS 9.0, *) {
      return SDKApplicationDelegate.shared.application(app, open: url, options: options)
    }

    return false
  }

and if I rename it tells me

Module FacebookCore has no member named ApplicationDelegate

FacebookCore has no member named ApplicationDelegate

What is the current name? According to the docs it's ApplicationDelegate, but compiler seems to disagree.

@hk0i
Copy link

hk0i commented Jun 19, 2019

Fixed it by changing my import from import FacebookCore to import FBSDKCoreKit (the ObjC library) then you can use ApplicationDelegate without namespace issues.

Explanation

After digging through the facebook sources I realized ObjC class has a macro to export the class as a different name to swift using NS_SWIFT_NAME:

NS_SWIFT_NAME(ApplicationDelegate)
@interface FBSDKApplicationDelegate : NSObject

Same for AppEvents:

NS_SWIFT_NAME(AppEvents)
@interface FBSDKAppEvents : NSObject

Both are only referenced within FBSDKCoreKit

Side note about AppEventsLogger

For AppEventsLogger.activate(Application) I'm not sure what to do but I replaced it with:

AppEvents.activateApp()

Which says is being called from FBSDKApplication.didBecomeActive but I don't see any such method in the code...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants