-
Notifications
You must be signed in to change notification settings - Fork 2k
/
FacebookConnectPlugin.m
executable file
·447 lines (387 loc) · 16.3 KB
/
FacebookConnectPlugin.m
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
// FacebookConnectPlugin.m
// GapFacebookConnect
//
// Created by Jesse MacFadyen on 11-04-22.
// Updated by Mathijs de Bruin on 11-08-25.
// Copyright 2011 Nitobi, Mathijs de Bruin. All rights reserved.
//
#import "FacebookConnectPlugin.h"
#import "FBSBJSON.h"
@interface FacebookConnectPlugin ()
<FBDialogDelegate>
@property (strong, nonatomic) Facebook *facebook;
@property (strong, nonatomic) NSString *userid;
@property (strong, nonatomic) NSString* loginCallbackId;
@property (strong, nonatomic) NSString* dialogCallbackId;
- (NSDictionary*) responseObject;
@end
@implementation FacebookConnectPlugin
@synthesize facebook = _facebook;
@synthesize userid = _userid;
@synthesize loginCallbackId = _loginCallbackId;
@synthesize dialogCallbackId = _dialogCallbackId;
/* This overrides CDVPlugin's method, which receives a notification when handleOpenURL is called on the main app delegate */
- (void) handleOpenURL:(NSNotification*)notification
{
NSURL* url = [notification object];
if (![url isKindOfClass:[NSURL class]]) {
return;
}
[FBSession.activeSession handleOpenURL:url];
}
/*
* Callback for session changes.
*/
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
case FBSessionStateOpenTokenExtended:
if (!error) {
// We have a valid session
if (nil == self.facebook) {
// Initiate a Facebook instance
self.facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
}
// Store the Facebook session information
self.facebook.accessToken = FBSession.activeSession.accessToken;
self.facebook.expirationDate = FBSession.activeSession.expirationDate;
if (state == FBSessionStateOpen) {
// Get the user's info
[FBRequestConnection startForMeWithCompletionHandler:
^(FBRequestConnection *connection, id <FBGraphUser>user, NSError *error) {
if (!error) {
self.userid = user.id;
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:
[self responseObject]];
NSString* callback = [pluginResult toSuccessCallbackString:self.loginCallbackId];
// we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation)
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
} else {
self.userid = @"";
}
}];
}
// Send the plugin result
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:
[self responseObject]];
NSString* callback = [pluginResult toSuccessCallbackString:self.loginCallbackId];
// we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation)
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
// Clear out the Facebook instance
self.facebook = nil;
self.userid = @"";
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
/*
* Check if a permision is a read permission.
*/
- (BOOL)isPublishPermission:(NSString*)permission {
return [permission hasPrefix:@"publish"] ||
[permission hasPrefix:@"manage"] ||
[permission isEqualToString:@"ads_management"] ||
[permission isEqualToString:@"create_event"] ||
[permission isEqualToString:@"rsvp_event"];
}
/*
* Check if all permissions are read permissions.
*/
- (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions {
for (NSString *permission in permissions) {
if ([self isPublishPermission:permission]) {
return NO;
}
}
return YES;
}
- (void) init:(CDVInvokedUrlCommand*)command
{
self.userid = @"";
// No need to use this right now, store it?
//NSString* appId = [command.arguments objectAtIndex:0];
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[super writeJavascript:[result toSuccessCallbackString:command.callbackId]];
}
- (void) getLoginStatus:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self responseObject]];
NSString* callback = [pluginResult toSuccessCallbackString:command.callbackId];
// we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation)
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
}
- (void) login:(CDVInvokedUrlCommand*)command
{
NSArray *permissions = nil;
if ([command.arguments count] > 0) {
permissions = command.arguments;
}
// save the callbackId for the login callback
self.loginCallbackId = command.callbackId;
// Check if the session is open or not
if (FBSession.activeSession.isOpen) {
// Reauthorize if the session is already open.
// In this instance we can ask for publish type
// or read type only if taking advantage of iOS6.
// To mix both, we'll use deprecated methods
BOOL publishPermissionFound = NO;
BOOL readPermissionFound = NO;
for (NSString *p in permissions) {
if ([self isPublishPermission:p]) {
publishPermissionFound = YES;
} else {
readPermissionFound = YES;
}
// If we've found one of each we can stop looking.
if (publishPermissionFound && readPermissionFound) {
break;
}
}
if (publishPermissionFound && readPermissionFound) {
// Mix of permissions, use deprecated method
[FBSession.activeSession
reauthorizeWithPermissions:permissions
behavior:FBSessionLoginBehaviorWithFallbackToWebView
completionHandler:^(FBSession *session, NSError *error) {
[self sessionStateChanged:session
state:session.state
error:error];
}];
} else if (publishPermissionFound) {
// Only publish permissions
[FBSession.activeSession
reauthorizeWithPublishPermissions:permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
[self sessionStateChanged:session
state:session.state
error:error];
}];
} else {
// Only read permissions
[FBSession.activeSession
reauthorizeWithReadPermissions:permissions
completionHandler:^(FBSession *session, NSError *error) {
[self sessionStateChanged:session
state:session.state
error:error];
}];
}
} else {
// Initial log in, can only ask to read
// type permissions if one wants to use the
// non-deprecated open session methods and
// take advantage of iOS6 integration
if ([self areAllPermissionsReadPermissions:permissions]) {
[FBSession
openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
} else {
// Use deprecated methods for backward compatibility
[FBSession
openActiveSessionWithPermissions:permissions
allowLoginUI:YES completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
}
[super writeJavascript:nil];
}
- (void) logout:(CDVInvokedUrlCommand*)command
{
if (!FBSession.activeSession.isOpen) {
return;
}
// Close the session and clear the cache
[FBSession.activeSession closeAndClearTokenInformation];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[super writeJavascript:[pluginResult toSuccessCallbackString:command.callbackId]];
}
- (void) showDialog:(CDVInvokedUrlCommand*)command
{
// Save the callback ID
self.dialogCallbackId = command.callbackId;
NSMutableDictionary *options = [command.arguments lastObject];
NSString* method = [[NSString alloc] initWithString:[options objectForKey:@"method"]];
if ([options objectForKey:@"method"]) {
[options removeObjectForKey:@"method"];
}
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (id key in options) {
if ([[options objectForKey:key] isKindOfClass:[NSString class]]) {
[params setObject:[options objectForKey:key] forKey:key];
} else {
// For optional ARC support
#if __has_feature(objc_arc)
FBSBJSON *jsonWriter = [FBSBJSON new];
#else
FBSBJSON *jsonWriter = [[FBSBJSON new] autorelease];
#endif
NSString *paramString = [jsonWriter stringWithObject:[options objectForKey:key]];
[params setObject:paramString forKey:key];
}
}
[self.facebook dialog:method andParams:params andDelegate:self];
// For optional ARC support
#if __has_feature(objc_arc)
#else
[method release];
[params release];
#endif
[super writeJavascript:nil];
}
- (void) dealloc
{
self.facebook = nil;
[super dealloc];
}
- (NSDictionary*) responseObject
{
NSString* status = @"unknown";
NSDictionary* sessionDict = nil;
NSTimeInterval expiresTimeInterval = [FBSession.activeSession.expirationDate timeIntervalSinceNow];
NSString* expiresIn = @"0";
if (expiresTimeInterval > 0) {
expiresIn = [NSString stringWithFormat:@"%0.0f", expiresTimeInterval];
}
if (FBSession.activeSession.isOpen) {
status = @"connected";
sessionDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:
FBSession.activeSession.accessToken,
expiresIn,
@"...",
[NSNumber numberWithBool:YES],
@"...",
self.userid,
nil]
forKeys:[NSArray arrayWithObjects:
@"accessToken",
@"expiresIn",
@"secret",
@"session_key",
@"sig",
@"userID",
nil]];
}
NSMutableDictionary *statusDict = [NSMutableDictionary dictionaryWithObject:status forKey:@"status"];
if (nil != sessionDict) {
[statusDict setObject:sessionDict forKey:@"authResponse"];
}
return statusDict;
}
/**
* A function for parsing URL parameters.
*/
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val =
[[kv objectAtIndex:1]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[params setObject:val forKey:[kv objectAtIndex:0]];
}
return params;
}
////////////////////////////////////////////////////////////////////
// FBDialogDelegate
/**
* Called when the dialog succeeds and is about to be dismissed.
*/
- (void)dialogDidComplete:(FBDialog *)dialog
{
// TODO
}
/**
* Called when the dialog succeeds with a returning url.
*/
- (void)dialogCompleteWithUrl:(NSURL *)url
{
// Send the URL parameters back, for a requests dialog, the "request" parameter
// will include the resutling request id. For a feed dialog, the "post_id"
// parameter will include the resulting post id.
NSDictionary *params = [self parseURLParams:[url query]];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
NSString* callback = [pluginResult toSuccessCallbackString:self.dialogCallbackId];
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
}
/**
* Called when the dialog get canceled by the user.
*/
- (void)dialogDidNotCompleteWithUrl:(NSURL *)url
{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
NSString* callback = [pluginResult toSuccessCallbackString:self.dialogCallbackId];
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
}
/**
* Called when the dialog is cancelled and is about to be dismissed.
*/
- (void)dialogDidNotComplete:(FBDialog *)dialog
{
// TODO
}
/**
* Called when dialog failed to load due to an error.
*/
- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError *)error
{
}
/**
* Asks if a link touched by a user should be opened in an external browser.
*
* If a user touches a link, the default behavior is to open the link in the Safari browser,
* which will cause your app to quit. You may want to prevent this from happening, open the link
* in your own internal browser, or perhaps warn the user that they are about to leave your app.
* If so, implement this method on your delegate and return NO. If you warn the user, you
* should hold onto the URL and once you have received their acknowledgement open the URL yourself
* using [[UIApplication sharedApplication] openURL:].
*/
- (BOOL)dialog:(FBDialog*)dialog shouldOpenURLInExternalBrowser:(NSURL *)url
{
// TODO: pass this back to JS
return NO;
}
@end