I've been grappling with obtaining a logged in user access token for quite some time. I initially faced challenges with it in JavaScript, so I switched to Objective-C and managed to succeed.
Following that, I referred to this RN guide: https://facebook.github.io/react-native/docs/native-modules-ios.html#sending-events-to-javascript on how to transfer a string from Objective-C to JavaScript, and it appeared to be successful. However, the frustrating part is that it keeps returning as undefined. This annoys me because I can see the proper return of the string in my Objective-C log, but in my JavaScript code after logging in, it reads as undefined.
In AppDelegate.m, here's what I have:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;
[self.bridge.eventDispatcher sendAppEventWithName:@"AccessToken"
body:@{@"name": fbAccessToken}];
NSLog(@"%@", fbAccessToken);
return handled;
}
This successfully logs a functioning access token. In JavaScript, I trigger the EventEmitter using:
var subscription = NativeAppEventEmitter.addListener('AccessToken');
, which I assume should work since I also referenced this example: https://github.com/facebook/react-native/blob/master/Libraries/Geolocation/Geolocation.js, where it seems they have followed a similar approach like mine.
Simply calling the subscriber provides me with this result:
2016-05-06 10:39:31.013 [info][tid:com.facebook.React.JavaScript] { subscriber:
{ _subscriptionsForType:
{ AccessToken:
[ { subscriber: [Circular],
listener: undefined,
context: undefined,
eventType: 'AccessToken',
key: 0 },
[Circular] ] },
_currentSubscription: null },
listener: undefined,
context: undefined,
eventType: 'AccessToken',
key: 1 }
So accessing name or body only gives me undefined
.
What am I missing here?