react-native icon indicating copy to clipboard operation
react-native copied to clipboard

sendDeviceEvent not working in AppDelegate (or more specific, not working if called in 3rd API callback)

Open xkangai opened this issue 11 years ago • 13 comments

hi, guys. I am doing a project in which basically I need get an 3rd party API callback userinfo in appDelegate before I send it to my js file. I can make up a fake userInfo in my
WeixinLoginManager.m and send it to my js successfully. But when i want to do it in the AppDelegate.m, it is not working.

BTW I am also wondering it is a good practice to do it in the AppDelegate.m? since the 3rd party library(wechat) asked me to receive the callback in AppDelegate. I do not whether I can make it possible to receive this in other m file.

2015-04-27 07 05 04 2015-04-27 07 05 52 2015-04-27 07 03 55

please help.

xkangai avatar Apr 26 '15 23:04 xkangai

i also post the question at http://stackoverflow.com/questions/29884741/react-native-senddeviceevent-not-working-in-appdelegate thx!

xkangai avatar Apr 26 '15 23:04 xkangai

Are you trying to emit the event before your JavaScript code has a chance to actually bootstrap?

jaygarcia avatar Apr 26 '15 23:04 jaygarcia

@jaygarcia ,can you be more specific? i am a newbie in react native.

xkangai avatar Apr 27 '15 00:04 xkangai

@watsonwu9,

If you look @ AppDelegate.m, you'll see where React Native's root view is initialized.

The question is: Are you firing the event before the root view is initialized?

jaygarcia avatar Apr 27 '15 12:04 jaygarcia

@watsonwu9 can you also post the code inside your AppDelegate?

If I understand correctly, when your JS application loads, you call sendAuthReqToWX to initialize the 3rd party API, and some time after that, it calls a delegate method on your App Delegate. In that App Delegate method, you then call sendUserInfoToJS on your module and send the user data back to the bridge - is that correct?

nicklockwood avatar Apr 27 '15 12:04 nicklockwood

@jaygarcia this is my appDelegate.m file. I have changed (BOOL)application:(UIApplication *)application handleOpenURL: and OpenURL methods and move all data processing methods into the weixinLogin Manager.m file. 2015-04-27 21 59 32 2015-04-27 21 59 17 2015-04-27 21 59 25 2015-04-27 21 59 10 2015-04-27 21 59 37

xkangai avatar Apr 27 '15 14:04 xkangai

@nicklockwood hi, man. to make flow clear, I have moved all data processing methods to the weixinLoginManager.m file. And what strikes me is that

if i call -(void)SendDataToJS from RCT_EXPORT_METHOD(sendAuthReqToWX) method [_bridge.eventDispatcher sendDeviceEventWithName executes and the data did pass to js; all are doing great. if i call -(void)SendDataToJS from -(void) onResp:(BaseResp*)resp ( a 3rd party API callback delegate method provided by wechat). the [_bridge.eventDispatcher sendDeviceEventWithName: method is not responding and nothing passes to the js.

bug_meitu_0

xkangai avatar Apr 27 '15 14:04 xkangai

WeixinLoginManager should ideally not be a singleton (i.e. should not be accessed via a sharedInstance method). The RCTBridge creates its own instance of WeixinLoginManager, so if you try to access [WeixinLoginManager sharedInstance], it won't be the same instance that the bridge is using.

Instead of calling [WeixinLoginManager sharedInstance], use bridge.modules[@"WeixinLoginManager"].

To do this, you'll need to have a reference to the bridge somewhere in your app delegate.

nicklockwood avatar Apr 27 '15 15:04 nicklockwood

@nicklockwood thx! As you said,how can i have a reference to the bridge somewhere in your app delegate? Should I set up the reference in the WeixinLoginManager.m file?

xkangai avatar Apr 27 '15 15:04 xkangai

Where do you set up your RCTRootView?

nicklockwood avatar Apr 27 '15 15:04 nicklockwood

@nicklockwood Should I set it up? it is a library file provided by react-native... what should i do with it? sorry I am very newbie into react native. 2015-04-27 23 52 42

xkangai avatar Apr 27 '15 15:04 xkangai

@watsonwu9 did you look at the example apps? Every app normally has an RCTRootView, which is typically set up in the AppDelegate. That's where your React Native app will appear on screen.

The RCTRootView has a bridge property, so if you set up your RCTRootView in the AppDelegate, you can store the bridge in a property in your AppDelegate and then use that to access you module.

nicklockwood avatar Apr 27 '15 16:04 nicklockwood

@nicklockwood ,it works! Many thanks!

xkangai avatar Apr 29 '15 01:04 xkangai