CocoaPods trunk is moving to be read-only. Read more on the blog, there are 11 months to go.

FacebookAuth 0.2.1

FacebookAuth 0.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2017
SwiftSwift Version 3.1
SPMSupports SPM

Maintained by Moritz Lang.



  • By
  • Moritz Lang

Facebook authentication for iOS in pure Swift without any internal dependencies 🚀

Installation

Usage

The main goal of FacebookAuth is it's simple usage. To get started you have to follow these three steps:

1. Create a new FacebookAuth instance

let config = FacebookAuth.Config(appID: "facebookAppID")
let facebookAuth = FacebookAuth(config: config)

2. Configure the instance to handle Facebook responses

In your AppDelegate, add the following method:

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

3. Configure your URL schemes

Open your Info.plist as "Source Code" and add the following snippet:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb<YourFacebookAppID></string>
        </array>
    </dict>
</array>

Make sure to replace <YourFacebookAppID> with your Faceboook application ID.

4. Call the authenticate method

Now the only thing left to do is to call authenticate on the FacebookAuth instance and the library should do the rest.

// self is the current `UIViewController`.
facebookAuth.authenticate(onTopOf: self, permissions: [.publicProfile, .email]) { result in
    guard let token = result.token else {
        guard let error = result.error else { return }
        switch error {
        case .cancelled:
            print("The authentication was cancelled by the user.")
        case .failed:
            print("Something else went wrong.")
        }
        return
    }
    guard let permissions = result.granted else { return }
    print("Token \(token) has the following permissions: \(permissions)")
}

Aksing for permissions

If you need more permissions later you can call the askForPermissions method and pass in the permissions you want to ask for.

facebookAuth.ask(for: [.userBirthday], onTopOf: self) { result in }

You can handle result exactly like above, where result.granted is an array of all permissions granted by the user.