-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMDBSwiftFacebookUtils.swift
205 lines (154 loc) · 7.45 KB
/
MDBSwiftFacebookUtils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// MDBSwiftFacebookUtils.swift
// SalePool
//
// Created by Akkshay Khoslaa on 5/20/16.
// Copyright © 2016 Akkshay Khoslaa. All rights reserved.
//
import Foundation
import FBSDKCoreKit
import FBSDKLoginKit
public class MDBSwiftFacebookUtils {
static var facebookFriends: Array<NSDictionary>!
/**
Gets all of the user's Facebook friends asynchronously. Block is called once all friends are retrieved.
- parameters:
- block: block that is executed once all friends are retrieved.
*/
static func getAllFBFriendsWithBlock(block: (Array<NSDictionary>) -> Void) {
facebookFriends = Array<NSDictionary>()
getFriendsUsingApp()
getFriendsNotUsingApp(nil, finalCompletion: block)
}
/**
Gets all of the user's Facebook friends asynchronously. Retrieves all friends in batches of 25 and executes the block after each batch is retrieved .
- parameters:
- block: block that is called each time a batch is retrieved.
*/
static func getAllFBFriendsWithIncrementalBlock(block: (Array<NSDictionary>) -> Void) {
facebookFriends = Array<NSDictionary>()
getFriendsUsingApp()
getFriendsNotUsingApp(block, finalCompletion: nil)
}
private static func getFriendsNotUsingApp(eachCompletion: ((Array<NSDictionary>) -> Void)?, finalCompletion: ((Array<NSDictionary>) -> Void)?) {
FBSDKGraphRequest(graphPath: "/me/invitable_friends", parameters: ["fields": "name, email, friends, picture"]).startWithCompletionHandler {
(connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
let data = result.objectForKey("data") as! NSArray
for i in 0...(data.count - 1) {
let infoDict : NSDictionary = data[i] as! NSDictionary
self.facebookFriends.append(infoDict)
}
if finalCompletion == nil {
dispatch_async(dispatch_get_main_queue(), {
eachCompletion!(self.facebookFriends)
})
}
if let paging = result.objectForKey("paging") as! NSDictionary? {
if let nextString = paging.objectForKey("next") as! String? {
self.recursiveRequestFriends(nextString, eachCompletion: eachCompletion, finalCompletion: finalCompletion)
}
}
}
}
private static func getFriendsUsingApp() {
let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: ["fields": "name, email, friends, picture"])
fbRequest.startWithCompletionHandler {
(connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
let resultDict = result as! NSDictionary
let data = resultDict.objectForKey("data") as! NSArray
for i in 0...(data.count - 1) {
let infoDict : NSDictionary = data[i] as! NSDictionary
self.facebookFriends.append(infoDict)
}
} else {
print("Error: \(error)");
}
}
}
private static func recursiveRequestFriends(urlString: String, eachCompletion: ((Array<NSDictionary>) -> Void)?, finalCompletion: ((Array<NSDictionary>) -> Void)?) {
httpJSONGETRequest(urlString) {
(result: NSDictionary) -> Void in
let data : NSArray = result.objectForKey("data") as! NSArray
for i in 0...(data.count - 1) {
let infoDict : NSDictionary = data[i] as! NSDictionary
self.facebookFriends.append(infoDict)
}
if finalCompletion == nil {
dispatch_async(dispatch_get_main_queue(), {
eachCompletion!(self.facebookFriends)
})
}
if let paging = result.objectForKey("paging") as! NSDictionary? {
if let nextString = paging.objectForKey("next") as! String? {
self.recursiveRequestFriends(nextString, eachCompletion: eachCompletion, finalCompletion: finalCompletion)
} else {
if eachCompletion == nil {
finalCompletion!(self.facebookFriends)
}
}
}
}
}
private static func httpJSONGETRequest(urlString: String, completion: (NSDictionary) -> Void) {
let url: NSURL = NSURL(string: urlString)!
let request = NSURLRequest(URL: url)
let opQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: opQueue) {
(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
do {
if let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
completion(result)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
/**
Get the current user's name, email, gender, and Facebook id in an NSDictionary.
- parameters:
- block: block that's called once data has been retrieved
*/
static func getFBDataWithBlock(block: (NSDictionary) -> Void) {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "name, email, gender"])
graphRequest.startWithCompletionHandler {
(connection, result, error) -> Void in
if error != nil {
NSLog("Error: \(error)")
} else {
block(result as! NSDictionary)
}
}
}
/**
Get the current user's Facebook profile picture as a UIImage.
- parameters:
- block: block that's called once picture has been retrieved
*/
static func getFBProfilePicWithBlock(block: (UIImage) -> Void) {
let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture?type=large&redirect=false", parameters: nil)
pictureRequest.startWithCompletionHandler {
(connection, result, error: NSError!) -> Void in
if error == nil {
if let profilePicture = result.objectForKey("data") as! NSDictionary? {
if let picUrl = profilePicture.objectForKey("url") as! String? {
let url = NSURL(string: picUrl)
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) {
(response, data, error) in
let image = UIImage(data: data!)
block(image!)
}
} else {
NSLog("Picture could not be retrieved")
}
} else {
NSLog("Picture could not be retrieved")
}
} else {
NSLog("Erorr: \(error)")
}
}
}
}