/
SignInViewController+Facebook.swift
60 lines (46 loc) · 1.77 KB
/
SignInViewController+Facebook.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// SignIn+Fb.swift
// Odoo Marketplace
//
// Created by bhavuk.chawla on 24/07/18.
// Copyright © 2018 bhavuk.chawla. All rights reserved.
//
import Foundation
import FBSDKLoginKit
class MobikulFBSignIn: NSObject{
override init() {
super.init()
let view = UIApplication.shared.delegate?.window
if let viewController = view??.rootViewController {
self.fbLogin(view: viewController)
}
}
func fbLogin(view: UIViewController) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile", "email", "user_friends"], from: view) { (result, error) -> Void in
if (error == nil){
if let fbloginresult : FBSDKLoginManagerLoginResult = (result)
{
if(fbloginresult.isCancelled){
}
else{
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
let Dict = result as? [String : Any]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MobikulFBSignIn"), object: nil, userInfo: Dict)
}
})
}
}
}