3737 This protocol exists so that we can weakly refer to messages to pass to PFFacebookUtils without
3838 actually taking a dependency on the symbols.
3939 */
40- @protocol WeaklyReferencedFBUtils
40+ @protocol WeaklyReferencedFBUtils < NSObject >
4141
42- + (void )logInWithPermissions : (NSArray *)permissions block : (void (^)(PFUser *user, NSError *error))block ;
42+ // FBSDKv3
43+ + (void )logInWithPermissions : (NSArray *)permissions block : (PFUserResultBlock)block ;
44+ // FBSDKv4
45+ + (void )logInInBackgroundWithReadPermissions : (NSArray *)permissions block : (PFUserResultBlock)block ;
46+ + (void )logInInBackgroundWithPublishPermissions : (NSArray *)permissions block : (PFUserResultBlock)block ;
4347
4448@end
4549
@@ -279,6 +283,8 @@ - (void)_requestPasswordResetWithEmail:(NSString *)email {
279283 }];
280284}
281285
286+ #pragma mark Log In With Facebook
287+
282288- (void )_loginWithFacebook {
283289 if (self.loading ) {
284290 return ;
@@ -289,23 +295,55 @@ - (void)_loginWithFacebook {
289295 [(PFActionButton *)_logInView.facebookButton setLoading: YES ];
290296 }
291297
292- Class fbUtils = NSClassFromString (@" PFFacebookUtils" );
293- [fbUtils logInWithPermissions: _facebookPermissions block: ^(PFUser *user, NSError *error) {
294- self.loading = NO ;
298+ __weak typeof (self) wself = self;
299+ PFUserResultBlock resultBlock = ^(PFUser *user, NSError *error) {
300+ __strong typeof (wself) sself = wself;
301+ sself.loading = NO ;
295302 if ([_logInView.facebookButton isKindOfClass: [PFActionButton class ]]) {
296303 [(PFActionButton *)_logInView.facebookButton setLoading: NO ];
297304 }
298305
299306 if (user) {
300- [self _loginDidSuceedWithUser: user];
307+ [sself _loginDidSuceedWithUser: user];
301308 } else if (error) {
302- [self _loginDidFailWithError: error];
309+ [sself _loginDidFailWithError: error];
303310 } else {
304311 // User cancelled login.
305312 }
306- }];
313+ };
314+
315+ Class fbUtils = NSClassFromString (@" PFFacebookUtils" );
316+ if ([fbUtils respondsToSelector: @selector (logInWithPermissions:block: )]) {
317+ // Facebook SDK v3 Login
318+ [fbUtils logInWithPermissions: _facebookPermissions block: resultBlock];
319+ } else if ([fbUtils respondsToSelector: @selector (logInInBackgroundWithReadPermissions:block: )]) {
320+ // Facebook SDK v4 Login
321+ if ([self _permissionsContainsFacebookPublishPermission: _facebookPermissions]) {
322+ [fbUtils logInInBackgroundWithPublishPermissions: _facebookPermissions block: resultBlock];
323+ } else {
324+ [fbUtils logInInBackgroundWithReadPermissions: _facebookPermissions block: resultBlock];
325+ }
326+ } else {
327+ [NSException raise: NSInternalInconsistencyException
328+ format: @" Can't find PFFacebookUtils. Please link with ParseFacebookUtils or ParseFacebookUtilsV4 to enable login with Facebook." ];
329+ }
330+ }
331+
332+ - (BOOL )_permissionsContainsFacebookPublishPermission : (NSArray *)permissions {
333+ for (NSString *permission in permissions) {
334+ if ([permission hasPrefix: @" publish" ] ||
335+ [permission hasPrefix: @" manage" ] ||
336+ [permission isEqualToString: @" ads_management" ] ||
337+ [permission isEqualToString: @" create_event" ] ||
338+ [permission isEqualToString: @" rsvp_event" ]) {
339+ return YES ;
340+ }
341+ }
342+ return NO ;
307343}
308344
345+ #pragma mark Log In With Twitter
346+
309347- (void )_loginWithTwitter {
310348 if (self.loading ) {
311349 return ;
@@ -332,6 +370,8 @@ - (void)_loginWithTwitter {
332370 }];
333371}
334372
373+ #pragma mark Log In
374+
335375- (void )_loginAction {
336376 if (self.loading ) {
337377 return ;
@@ -522,7 +562,7 @@ - (void)_updateViewContentOffsetAnimated:(BOOL)animated {
522562 contentOffset = CGPointMake (0 .0f , MIN (offsetForScrollingTextFieldToTop,
523563 offsetForScrollingLowestViewToBottom));
524564 }
525-
565+
526566 [_logInView setContentOffset: contentOffset animated: animated];
527567}
528568
0 commit comments