Skip to content

Nothing but Facebook login, entirely written in Swift 🐥

License

Notifications You must be signed in to change notification settings

slashkeys/FacebookAuth

Repository files navigation

FacebookAuth

Build Status Coverage Status Swift Version Carthage compatible

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

Installation

Carthage

Add the following to your Cartfile:

github "slashkeys/FacebookAuth"

Cocoapods

Add the following to your Podfile:

pod 'FacebookAuth'

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.