0

After integrating with facebook when I tap on continue with FaceBook button on simulator, it popups this window on simulator..and when I tap on back to home it redirects me to login window and allows me to scroll in facebook insted of redirecting back to my app, and I am not getting any user data with it.this

Here is steps that I have done

  1. Pod installation: pod 'FBSDKLoginKit'
  2. Added Bundle ID in quickstart guide in developer.facebook
  3. Enable Single Sign option is selected to NO
  4. Added the following code as it is in info.plist info.plist

Added the following code in AppDelegate

According to Facebook document we have to import FacebookCore in AppDelegate when we Import it we get warning No such module 'FacebookCore'

import FBSDKCoreKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        ApplicationDelegate.shared.application(
                    application,
                    didFinishLaunchingWithOptions: launchOptions
                )
        return true
    }

func application(
            _ app: UIApplication,
            open url: URL,
            options: [UIApplication.OpenURLOptionsKey : Any] = [:]
        ) -> Bool {
            ApplicationDelegate.shared.application(
                app,
                open: url,
                sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                annotation: options[UIApplication.OpenURLOptionsKey.annotation]
            )
        }

Added the following code in SceneDelegate

According to Facebook document we have to import FacebookCore in SceneDelegate when we Import it we get warning No such module 'FacebookCore'

import FBSDKCoreKit  
        
        func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
                guard let url = URLContexts.first?.url else {
                    return
                }
                ApplicationDelegate.shared.application(
                    UIApplication.shared,
                    open: url,
                    sourceApplication: nil,
                    annotation: [UIApplication.OpenURLOptionsKey.annotation]
                )
            }

Added the following code in ViewController

According to Facebook document we have to import FacebookLogin in ViewController when we Import it we get warning No such module 'FacebookLogin'

import FBSDKLoginKit
        
        class ViewController: UIViewController, LoginButtonDelegate
                override func viewDidLoad() {
                    super.viewDidLoad()
                    // Do any additional setup after loading the view.
                    if let token = AccessToken.current,
                            !token.isExpired {
                        let token = token.tokenString
                        let request = FBSDKLoginKit.GraphRequest(graphPath: "me", parameters: ["fields": "email,name"], tokenString: token, version: nil, httpMethod: .get)
                        request.start { connecton, result, error in
                            print("\(result)")
                        }
                    } else{
                        let loginButton = FBLoginButton()
                                loginButton.center = view.center
                        loginButton.delegate = self
                        loginButton.permissions = ["public_profile", "email"]
                                view.addSubview(loginButton)
                    }
                }
            
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
    let token = result?.token?.tokenString
    let request = FBSDKLoginKit.GraphRequest(graphPath: "me", parameters: ["fields": "email,name"], tokenString: token, version: nil, httpMethod: .get)
    request.start { connecton, result, error in
        print("\(result)")
    }
}

I have tried step 2 from this but I am getting this error while setting development environment.

Sarthak
  • 29
  • 1
  • 6

1 Answers1

3

I think you've mistaken the Facebook login step.

step 1:- install the below pods

pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'

step 2:- add necessary facebook credentials (facebook App Id) and CFBundleURLTypes in info.plist file

enter image description here

enter image description here

step 3:- import FacebookCore and FacebookLogin in your UIViewController where you want the Facebook login feature.

call this method for login with facebook.

func setupFacebookLogin(){
    let loginManager = LoginManager()
    loginManager.logOut()
    loginManager.logIn(permissions: ["public_profile", "email"], from: self, handler: { result, error in
        if error != nil {
            print("ERROR: Trying to get login results")
        } else if result?.isCancelled != nil {
            print("The token is \(result?.token?.tokenString ?? "")")
            if result?.token?.tokenString != nil {
                print("Logged in")
                let graphRequest: GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "id, first_name, middle_name, last_name, name, picture, email, gender, birthday"])
                graphRequest.start {  _, result, error in
                    if error != nil {
                        let data: [String: AnyObject] = result as! [String: AnyObject]
                        print("Facebook user data - ",data)
                }
            } else {
                print("Cancelled")
            }
        }
    })
}
Shabnam Siddiqui
  • 579
  • 4
  • 13
  • do we have to add CLIENT-TOKEN from Settings > Advanced > Client Token...? – Sarthak Apr 15 '22 at 07:28
  • Do I have to remove all the code from viewdidLoad and call this method in viewDidLoad ? – Sarthak Apr 15 '22 at 07:30
  • to commence, add the Facebook app id, and URL scheme in info.plist file. then call this method in `viewDidLoad`. – Shabnam Siddiqui Apr 15 '22 at 07:35
  • Client Token token is not required at all – Shabnam Siddiqui Apr 15 '22 at 07:35
  • I have to remove all my previous code from ViewDidload and call you function ....and can you please specify where I have to call this function in my existing ViewDidload ? – Sarthak Apr 15 '22 at 07:51
  • you can call this on any of the buttons clicks. – Shabnam Siddiqui Apr 15 '22 at 07:56
  • now i am getting warning on simulator: App not setup..this app is still in development mode and you dont have access to it switch to registered test user or ask an app admin for permission. – Sarthak Apr 15 '22 at 08:06
  • go to the Facebook developer console and on the top of the screen there is one toggle button `app development: development.` make it live. – Shabnam Siddiqui Apr 15 '22 at 08:11
  • when I tap on toggle button it shows : Invalid Privacy Policy URL You must provide a valid Privacy Policy URL in order take your app Live. Go to Basic Settings and make sure it is valid. – Sarthak Apr 15 '22 at 08:13
  • add any URL it will be live – Shabnam Siddiqui Apr 15 '22 at 08:14
  • I have an LoginButtonDelegate function func loginButton() in my VC...If I removed all the code from func loginButton() and add your code func setupFacebookLogin()..will it work..? you can view that function in question – Sarthak Apr 15 '22 at 08:33
  • can you please help me[here](https://stackoverflow.com/q/72016301/18362158) – Sarthak Apr 26 '22 at 18:49