This is a demo app and a helper function for loading Facebook user profile:
- Facebook user ID
- Access token
- First name
- Last name
- Name
Facebook user ID and access token can be used to authenticate Facebook user in your app. The helper function can be useful for those who just need to load a Facebook user profile from code without using other SDK features.
- Open
Info.plist
file. - Update "Bundle identifier", "URL types", "FacebookAppID" and "FacebookDisplayName" with your facebook app information. See Facebook iOS SDK for more details.
- Add Facebook SDK to your app. Follow instructions on Facebook developer pages. See Facebook iOS SDK
- Add FacebookUserLoader.swift and TegFacebookUser.swift to your project.
Alternatively, if you are using Carthage, you can add the following to your Cartfile and run carthage update
github "marketplacer/load-facebook-profile-ios-swift" ~> 3.0
import LoadFacebookProfileKit
let loader = FacebookUserLoader() // Keep strong reference
...
loader.load(askEmail: true,
onError: { },
onSuccess: { user in
// User has logged in with Facebook
}
)
Please note that user may deny sharing all or some of the profile information. There is no guarantee, for example, that your app will get email address.
In order to authenticate a user one needs to verify its facebook user id
on server side. Send the following request from your server and compare the returned user id with the one loaded by TegFacebookUserLoader.load
function.
https://graph.facebook.com/me?fields=id&access_token=YOUR_ACCCESS_TOKEN
Sometimes it is useful to bypass the Facebook login in Facebook. Here is how to setup fake Facebook responses that will be used when calling load
method of FacebookUserLoader
object.
// Call `onSuccess` with the supplied user without touching Facebook SDK.
FacebookUserLoader.simulateSuccessUser = TegFacebookUser(id: "fake user id",
accessToken: "test access token",
email: "[email protected]",
firstName: "test first name",
lastName: "test last name",
name: "test name"
)
// Delay used to simulate Facebook response. If 0 response is returned synchronously.
FacebookUserLoader.simulateLoadAfterDelay = 0.1
// Call `onError` function without touching Facebook SDK.
FacebookUserLoader.simulateError = true