Шаг 1. Добавьте модули, как показано ниже
pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
pod 'FBSDKShareKit'
Шаг 2. Добавьте в файл Info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb<Your App id></string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string><Your App id></string>
<key>FacebookDisplayName</key>
<string><Your DisplayName></string>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
Шаг 3. Добавьте в свой AppDelegate
import FBSDKCoreKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Facebook
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return handled
}
Шаг 4. Добавьте это в свой View Controller
import FBSDKLoginKit
class LoginScreenViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
btn_Facebook.addTarget(self, action: #selector(handleCustomFBLogin), for: .touchUpInside)
}
///FACEBOOK LOGIN
func handleCustomFBLogin(sender:UIButton!){
FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, err) in
if(err != nil){
print("Custom FB Login Failed")
return
}
//print(result?.token.tokenString)
self.showEmailAddress()
}
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!){
if(error != nil){
print(error)
return
}
print("Successfully Logged in using facebook")
showEmailAddress()
}
func showEmailAddress(){
let accesstoken = FBSDKAccessToken.current();
guard let accessTokenString = accesstoken?.tokenString else {return}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields" : "id, name, first_name, last_name, email, birthday, picture"]).start { (connection, result, err) in
if(err != nil){
print("Failed to start GraphRequest", err ?? "")
return
}
print(result ?? "")
}
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!){
print("Logged out of Facebook")
}
}