Search code examples
androidiosreact-nativekontakt.io

how to single react native kontaktio for android and ios


How do I make a single component (say MainContent.js) that branches off according to the Platform to start scanning for beacons, with the kontaktio ios api, on an iphone, but with the kontaktio android api on an android phone?

The scanning is configured and callbacks are set up, in componentDidMount(), and also the scanning itself is started in componentDidMount().

But the ios kontaktio api uses NativeEmitter and has a very different api from the android kontaktio api which uses DeviceEmitter and has very different events, methods, and structures, (for example using connect() instead of init()).

I cannot do this in one code file, because I need to import different things and set different constants for each platform. But the results are the same. I identify a beacon nearby and set some state. (for example through Redux).

There's probably a simple way to do it, and this is most likely a stupid question, but somehow I cannot think of a way to do this since the components are not visual and there is no "render".


Solution

  • OK. From these answers here: Can I require a module specifically for iOS in React Native? I learned to make an AppMain.ios.js and an AppMain.android.js and automatically react native chooses the correct one.

    I had to uninstall the latest app on my device, and then did a gradlew clean in the android directory, and then ran on device as usual and everythings ok. Similarly for the iphone.