Created
June 16, 2021 22:09
-
-
Save meenabassem/5c25c9f964df861570deecf2558a2641 to your computer and use it in GitHub Desktop.
EXFacebook.m - expo-facebook - iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Source: https://github.com/expo/expo/blob/master/packages/expo-facebook/ios/EXFacebook/EXFacebook.m#L127-L177 | |
UM_EXPORT_METHOD_AS(logInWithReadPermissionsAsync, | |
logInWithReadPermissionsWithConfig:(NSDictionary *)config | |
resolver:(UMPromiseResolveBlock)resolve | |
rejecter:(UMPromiseRejectBlock)reject) | |
{ | |
if (![FBSDKSettings appID]) { | |
reject(EXFacebookMisconfiguredErrorDomain, @"No appId configured, required for initialization. Please ensure that you're either providing `appId` to `initializeAsync` as an argument or inside Info.plist.", nil); | |
return; | |
} | |
NSArray *permissions = config[@"permissions"]; | |
if (!permissions) { | |
permissions = @[@"public_profile", @"email"]; | |
} | |
// FB SDK requires login to run on main thread | |
// Needs to not race with other mutations of this global FB state | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init]; | |
[loginManager logOut]; | |
@try { | |
[loginManager logInWithPermissions:permissions fromViewController:nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { | |
if (error) { | |
reject(EXFacebookLoginErrorDomain, @"Error with Facebook login", error); | |
return; | |
} | |
if (result.isCancelled || !result.token) { | |
resolve(@{ @"type": @"cancel" }); | |
return; | |
} | |
NSMutableDictionary *accessToken = [NSMutableDictionary dictionaryWithDictionary:[EXFacebook accessTokenNativeToJSON:result.token]]; | |
accessToken[@"type"] = @"success"; | |
resolve(accessToken); | |
}]; | |
} | |
@catch (NSException *exception) { | |
NSError *error = [[NSError alloc] initWithDomain:EXFacebookLoginErrorDomain code:650 userInfo:@{ | |
NSLocalizedDescriptionKey: exception.description, | |
NSLocalizedFailureReasonErrorKey: exception.reason, | |
@"ExceptionUserInfo": exception.userInfo, | |
@"ExceptionCallStackSymbols": exception.callStackSymbols, | |
@"ExceptionCallStackReturnAddresses": exception.callStackReturnAddresses, | |
@"ExceptionName": exception.name | |
}]; | |
reject(error.domain, exception.reason, error); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment