Home>
I want to implement Facebook login Version

macOS mojave10.14.6
Xcode10.2

This article shows a rough setup, but this It doesn't work even if you go through it.

①Register the app on facebook for developers and select Quick Start

② Select iOS

③ Go from 1.

● 1. Set up the development environment
Open Podfile and

pod 'FBSDKLoginKit'


Is added.

Open a terminal and specify the project folder

$pod install


Run.

Click "Next".

● 2.Add a bundle ID
Bundle identifier for the project

Enter `` Save '' → `` Next ''

● Configure 4a.info.plist
Right-click on "info.plist" of the project and select "Open As"->"Source Code"

Add the following

<key>CFBundleURLTypes</key>

<Dict>
<Key>CFBundleURLSchemes</key>

<String>fb719968188486104</string>
</Array>
</Dict>
</array>
<key>FacebookAppID</key>
<string>719968188486104</string>
<key>FacebookDisplayName</key>
<string>Oh-gili</string>


● 4b. Build settings
Add "-ObjC" for all build targets of "Other Linker Flags" in "Build Setting"

"Next"

● 5. Connect to the app delegate
Here, for some reason, the sample is written in Ovjective-C, not Swift, and I can lose my motivation.

// AppDelegate.m
#import<FBSDKCoreKit/FBSDKCoreKit.h>
-(BOOL) application: (UIApplication *) application
    didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
  [[FBSDKApplicationDelegate sharedInstance] application: application
    didFinishLaunchingWithOptions: launchOptions];
  // Add any custom logic here.
  return YES;
}
-(BOOL) application: (UIApplication *) application
            openURL: (NSURL *) url
            options: (NSDictionary<UIApplicationOpenURLOptionsKey, id>*) options {
  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application: application
    openURL: url
    sourceApplication: options [UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation: options [UIApplicationOpenURLOptionsAnnotationKey]
  ];
  // Add any custom logic here.
  return handled;
}


However, I try to translate it based on information on the Internet.

At AppDelegate.swift

import UIKit
import FBSDKCoreKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application (_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)->Bool {
        // setupFBSDK
        ApplicationDelegate.shared.application (application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }
    func application (_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any)->Bool {
        // Facebook
        ApplicationDelegate.shared.application (application, open: url, sourceApplication: sourceApplication, annotation: annotation)
        return true
    }
    func applicationDidBecomeActive (_ application: UIApplication) {
        AppEvents.activateApp ()
    }


● 6. Add Facebook login button.
Again, it is written in Ovjective-C, so I translate it myself. Reference article

import UIKitimport FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, LoginButtonDelegate {
    override func viewDidLoad () {
        super.viewDidLoad ()
    }
    override func viewDidAppear (_ animated: Bool) {
        // Check if logged in
        if let _ = AccessToken.current {
            //Screen transition
            performSegue (withIdentifier: "modalTop", sender: self)
        } else {
            // FB login button installed
            let fbLoginBtn = FBLoginButton ()
            fbLoginBtn.permissions = ["public_profile", "email"]
            fbLoginBtn.center = self.view.center
            fbLoginBtn.delegate = self
            self.view.addSubview (fbLoginBtn)
        }
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
    }
    // Login callback
    func loginButton (_ loginButton: FBLoginButton !, didCompleteWith result: LoginManagerLoginResult !, error: Error!) {
        //Error checking
        if error == nil {
            // whether canceled
            if result.isCancelled {
                print ("Cancel")
            } else {
                //Screen transition
                performSegue (withIdentifier: "modalTop", sender: self)
            }
        } else {
            print ("error")
        }
    }
    // logout callback
    func loginButtonDidLogOut (_ loginButton: FBLoginButton!) {
    }
}


The information on the net also changed in some places, and I fixed it while fixing it.

A button like this should be centered.

I ’m no longer a question,

If it is a question, is it correct in this way of implementation?
Please let me know if there are any deficiencies.

  • Answer # 1

    As a result of changing the way to write round throw questions, I ended up self-solving. So put it above.

  • Answer # 2

    I don't know how to convert to swift, but

    Google ’s answer

Trends