Facebook authentication for iOS in pure Swift without any internal dependencies 🚀
Add the following to your Cartfile:
github "slashkeys/FacebookAuth"
Add the following to your Podfile:
pod 'FacebookAuth'
The main goal of FacebookAuth is it's simple usage. To get started you have to follow these three steps:
let config = FacebookAuth.Config(appID: "facebookAppID")
let facebookAuth = FacebookAuth(config: config)
In your AppDelegate
, add the following method:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
return facebookAuth.handle(url)
}
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.
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)")
}
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.