-
Notifications
You must be signed in to change notification settings - Fork 2
/
FacebookConnectPlugin.m
executable file
·344 lines (282 loc) · 11.2 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
//
// 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 "JSON.h"
#define APP_SECRET @"IGNORE"
@implementation FacebookConnectPlugin
@synthesize facebook, loginCallbackId;
/* 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;
}
[facebook handleOpenURL:url];
}
- (void) init:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
if ([arguments count] < 2) {
return;
}
NSString* callbackId = [arguments objectAtIndex:0];
NSString* appId = [arguments objectAtIndex:1];
self.facebook = [[Facebook alloc] initWithAppId:appId andDelegate: self];
// Check for any stored session update Facebook session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
self.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[super writeJavascript:[result toSuccessCallbackString:callbackId]];
}
- (void) getLoginStatus:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* callbackId = [arguments objectAtIndex:0]; // first item is the callbackId
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self responseObject]];
NSString* callback = [pluginResult toSuccessCallbackString: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:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
if ([arguments count] < 2 || !self.facebook) {
return;
}
NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
NSMutableArray* marray = [NSMutableArray arrayWithArray:arguments];
[marray removeObjectAtIndex:0]; // first item is the callbackId
// save the callbackId for the login callback
self.loginCallbackId = callbackId;
return [facebook authorize:marray];
[super writeJavascript:nil];
}
- (void) logout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
if (!self.facebook) {
return;
}
NSString* callbackId = [arguments objectAtIndex:0]; // first item is the callbackId
[facebook logout];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
- (void) showFeedPublishDialog:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* callbackId = [arguments objectAtIndex:0]; // first item is the callbackId
[facebook dialog:@"feed" andDelegate:self];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
NSString* callback = [pluginResult toSuccessCallbackString:callbackId];
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
}
- (void) showDialog:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* callbackId = [arguments objectAtIndex:0]; // first item is the callbackId
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 {
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSString *paramString = [jsonWriter stringWithObject:[options objectForKey:key]];
[params setObject:paramString forKey:key];
}
}
[facebook dialog:method andParams:params andDelegate:self];
[method release];
[params release];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
NSString* callback = [pluginResult toSuccessCallbackString:callbackId];
[super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]];
}
- (void) dealloc
{
self.facebook = nil;
[super dealloc];
}
- (NSDictionary*) responseObject
{
NSString* status = @"unknown";
NSDictionary* sessionDict = nil;
NSTimeInterval expiresTimeInterval = [self.facebook.expirationDate timeIntervalSinceNow];
NSString* expiresIn = @"0";
if (expiresTimeInterval > 0) {
expiresIn = [NSString stringWithFormat:@"%0.0f", expiresTimeInterval];
}
if (self.facebook && [self.facebook isSessionValid]) {
status = @"connected";
sessionDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:
self.facebook.accessToken,
expiresIn,
APP_SECRET,
[NSNumber numberWithBool:YES],
@"...",
@"...",
nil]
forKeys:[NSArray arrayWithObjects:
@"accessToken",
@"expiresIn",
@"secret",
@"session_key",
@"sig",
@"userID",
nil]];
} else {
sessionDict = [[NSDictionary new] autorelease];
}
NSDictionary* statusDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
status,
sessionDict,
nil]
forKeys:[NSArray arrayWithObjects:
@"status",
@"authResponse",
nil]];
return statusDict;
}
/**
* Called when the user successfully logged in.
*/
- (void) fbDidLogin
{
// Store session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[self.facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[self.facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
[facebook requestWithGraphPath:@"me" andDelegate:self];
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]];
}
- (void)fbDidLogout {
// Cleared stored session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbDidNotLogin:(BOOL)cancelled {
}
- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt {
// Updated stored session information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
[defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbSessionInvalidated {
}
////////////////////////////////////////////////////////////////////
// FBRequestDelegate
///**
// * Called just before the request is sent to the server.
// */
//- (void)requestLoading:(FBRequest *)request
//{
//
//}
///**
// * Called when the server responds and begins to send back data.
// */
//- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
//{
//
//}
/**
* Called when an error prevents the request from completing successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
}
/**
* Called when a request returns and its response has been parsed into
* an object. This is called only by the Graph API call to get the UID
*
* The resulting object may be a dictionary, an array, a string, or a number,
* depending on thee format of the API response.
*/
- (void) request:(FBRequest *)request didLoad:(id)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]];
}
///**
// * Called when a request returns a response.
// *
// * The result object is the raw response from the server of type NSData
// */
//- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
//{
//
//}
////////////////////////////////////////////////////////////////////
// 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
{
// TODO
}
/**
* Called when the dialog get canceled by the user.
*/
- (void)dialogDidNotCompleteWithUrl:(NSURL *)url
{
// TODO
}
/**
* 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