-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountManager.swift
More file actions
62 lines (54 loc) · 2.12 KB
/
AccountManager.swift
File metadata and controls
62 lines (54 loc) · 2.12 KB
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
57
58
59
60
61
62
//
// AccountManager.swift
// NotesApp
//
// Created by Carmen Popa on 29/08/2017.
// Copyright © 2017 Carmen Popa. All rights reserved.
//
import UIKit
private let Permissions = ["public_profile", "email", "user_friends"]
class AccountManager: NSObject {
func requestLogin(done: @escaping ()->()) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: Permissions, from: nil) { (result, error) in
if error == nil {
let fbLoginResult = result
if fbLoginResult?.grantedPermissions?.contains("email") != nil {
self.getUserData(done: done)
}
}
}
}
func doLogout () {
let loginManager = FBSDKLoginManager()
loginManager.logOut()
if let currentAccount = AccountDA().getAccount() {
AccountDA().deleteAccount(currentAccount)
}
}
func getUserData(done: @escaping ()->()) {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
let newAccount = AccountDA().createAccount()
if error != nil {
print("Error: \(String(describing: error))")
} else {
let name = (result as! [String: Any])["name"] as! String
if let userID = FBSDKAccessToken.current().userID {
NoteAPI().requestAuthentication(userID: userID, success: { (token) in
newAccount.set(name: name, id: userID, token: token)
AccountDA().save()
done()
}, failure: { (error) in
print(error)
})
}
}
})
}
func getProfileImage(id: String) -> NSURL {
let imgURLString = "http://graph.facebook.com/" + id + "/picture?type=large&width=80&height=80"
let imgURL = NSURL(string: imgURLString)
return imgURL!
}
}