I'm building a React Native native module (aka 'bridge') for iOS and Android and need to log to the JS console in native code (Objective C and Java). This is done easily in iOS using RCTLog but I can't figure out how to do it in Java.
I tried Log.i but those messages aren't forwarded to Javascript.
The only thing I can think of at this point is to emit a JS event from the Java module and have a handler on the JS side just call console.log with the message but that's a pretty roundabout way of doing it.
I would expect to be able to do something like this.getReactApplicationContext().log("see me in the JS console") but no dice. Anyone know how to do this?
It looks like as of upcoming react native 0.63 we may be getting the equivalent of iOS's RCTLog in Android: https://github.com/facebook/react-native/commit/52b3105f652eca72892f200923e1687f1d995486
The files are in master: https://github.com/facebook/react-native/tree/master/ReactAndroid/src/main/java/com/facebook/react/util
public void logEvent(String text) {
System.out.print(String.format("logEvent: %s ", text));
}
or just
System.out.print("Hello");
Related
I am a newbie to react native.
1.) I need to know if I can create a react native application and import it to Xcode, then add code in swift?
2.) I also need to know if the components I developed with swift can be compiled and run as a Android application?
Yes you can definitely do this, it's pretty common.
No, that swift code will not run on android
To help understand this, all UI code written in React Native is translated into corresponding native languages, everything else stays as Javascript.
Your Swift code cannot be translated back into Javascript that can then run on Android and certainly cannot be translated into Java.
I'm using Crashlytics (Natively) in React Native.
In RN, I'm overriding the JSExceptionHandler to use NativeModules and call out to my MyCrashHandler.
MyCrashHandler does the with Crashlytics reportError.. but this is only good if I THEN have a crash.
I can't have a Crashlytics crash because I already overrode the JSExceptionHandler.
How do I force Crashlytics to send my events? Maybe I need to use Firebase logging instead?
You don't have to wait for a crash to know that Crashlytics is working. You can use the SDK to force a crash by adding the following code to your app
Button crashButton = new Button(this);
crashButton.setText("Crash!");
Also, you can use your own API like this:
Fabric.with([Crashlytics.start(withAPIKey: "YOUR_API_KEY")]
You can write your own custom crash report by writing code in native iOS and Android. Please check the following the link for it.
https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios
We used to have a Cordova app that, when running on Appium, we could switch to Webview and execute JS commands using execute_script.
I would like to do the same thing on React Native to run some JS code (e.g., exposing a function on global or running something like console.disableYellowBox = true;). However, appium does not shows a Webview context to switch to and it seems to me the execute_script on native app context doesn't work.
Is there a way of doing a similar thing on RN client?
For ReactNative application there is no Webview context as RN is interpreted as NATIVE_APP one.
For finding elements its more interesting: there is no way to set resource-id for Android.
However you can set accessibilityLabel for your Views in React Native app and search for it like:
driver.findElementByAccessibilityId(accessibilityLabel)
Should work for both iOS/Android. Basically you should right your tests like you test Native app, not a Hybrid one.
And of course, you can use Xpath to search by text, but that I strongly do not recommend to do.
Is it possible that I can embed some native code with cordova ?
i.e In native application I can Intercept some javascript in webview and run native code
Is it possible in corodova ?
As far as I know it's not possible.Is there any plugin ? or something like that.
Yes it is possible.
Please refer here
http://docs.phonegap.com/tutorials/develop/1-embed-webview/ios/
How to call a JavaScript function from native Android module in Titanium. I want to register a JavaScript function in application view to listen for some events in native module and call back that function from native when that event happens.
The ModDevGuide by Appcelerator is a great place to learn how to do things like this. It's available here for both iOS and Android:
https://github.com/appcelerator/titanium_modules/tree/master/moddevguide/mobile
The "KrollDemoProxy" is what you want to look closer at.
On Android, the native side is here:
https://github.com/appcelerator/titanium_modules/blob/master/moddevguide/mobile/android/src/ti/moddevguide/KrollDemoProxy.java
On iOS, the native side is here:
https://github.com/appcelerator/titanium_modules/blob/master/moddevguide/mobile/ios/Classes/TiModdevguideKrollDemoProxy.m
And this is consumed by JavaScript like this, for both platforms:
https://github.com/appcelerator/titanium_modules/blob/master/moddevguide/mobile/android/example/demos/krollCallbacksAndEventsDemo.js
Search for the word "success" in those files, and you'll be able to figure out how it's firing that "success" event back to JavaScript-land.
Hope this helps.