How to go to specific native view controller from react-native code? - ios

I'm a newbie in react-native. I'm adding one feature in react-native to an existing swift application. I presented the RCTRootview from my native view controller. From there when user clicks on back button I have to go to Homepage which is written in swift. How do I communicate from react-native to native application code. Can someone please help me with this scenerio. I stuck at this point from last 2 days. Thanks in advance.

Yes. I found out the answer for this. It is all possible with RCTBridgeModule. This piece of code illustrate how to do that.
#import "CalendarManager.h"
#import <React/RCTLog.h>
#implementation CalendarManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(#"Pretending to create an event %# at %#", name, location);
}
Now, from your JavaScript file you can call the method like this:
import {NativeModules} from 'react-native';
var CalendarManager = NativeModules.CalendarManager;
CalendarManager.addEvent('Birthday Party', '4 Privet Drive, Surrey');
Please follow the below official link about the concept and how to do it.

Related

My custom native module is not present inside NativeModules object

So, i wanted to create a native module which will detect, if the app is running on emulator/simulator or an actual device.
Everything works fine on android, but i'm facing issue on iOS.
I have create a AbcModule.h and a AbcModule.m file
#import <React/RCTBridgeModule.h>
#interface AbcModule : NSObject <RCTBridgeModule>
#end
This is AbcModule.h
#import "AbcModule.h"
#implementation AbcModule
RCT_EXPORT_MODULE(GetDetails);
- (BOOL) xyzFunctn {
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
return NO;
#endif
}
RCT_EXPORT_METHOD(xyzFunctn: (RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject) {
resolve self.xyzFunctn;
}
#end
This is AbcModule.m
Here i have followed the react native documentation for implementing the Native Modules.
But i'm consistently facing this error which says
"TypeError null is not an object, evaluating GetDetails.xyzFunctn"
I have went through several solutions and articles but nothing seems to be working here.
Need help guys!
from the docs
If you do not specify a name, the JavaScript module name will match the Objective-C class name, with any "RCT" or "RK" prefixes removed.
so just do not specify any name,
#implementation AbcModule
// To export a module named AbcModule
RCT_EXPORT_MODULE();
#end
in your case it should then be accessible from within JS with
AbcModule
But the documentation is not clear if the Objective-C Class declaration needs to be written with prefixed "RCT" or "RK".. but because both prefixes seem to be valid, you should be able to just use AbcModule without prefix.
In other words, if you want to use GetDetails from within JS you need to name your interface and implementation accordingly
#implementation RCTGetDetails
RCT_EXPORT_MODULE(GetDetails);
// or
// RCT_EXPORT_MODULE();
#end
Okay, if there is someone who is facing this issue and feels like their code should work but it isn't and any solution online not working for you as well.
Try this:
When you create your .h and .m file for header and objective-c or swift file, make sure you do it in Xcode and not from VSCode.
VSCode eventually doesn't adds you .h file in the required resources folder, i have wasted my 2 weeks trying to find out solution for it, but lastly, that was it, yes this is it.
in your .m file, let's say GetDetails is a class of NSObject .swift
you need:
#interface RCT_EXTERN_MODULE(WidgetManager, NSObject)
// method to export
RCT_EXTERN_METHOD(isAuthenticated: (BOOL *)isAuthenticated)
#end
in your GetDetails.swift:
#objc(GetDetails)
class GetDetails: NSObject {
#objc(isAuthenticated:)
func isAuthenticated(_ isAuthenticated: Bool) {
}
}

Accessing the same persistent store from Swift and React Native

We're considering using React Native for one of our Screens - for fun, to check it and to get a better grasp of the possibilities, but even before w start I need some answers, so we're sure React Native and it's environments can meet our requirements.
In this question I'd like to specifically ask about persisting data and accessing it.
In our app we focus on offline experience, as it's one of the most crucial points for us. On iOS we have a couple of possibilities how to achieve this eg. Core Data, Realm... But the thing is if we decide to implement some part of our app as React Native we'd also need these parts to access stored data and even modify it and save to persistent store.
Can this be achieved? Having one persistent store (SQLite, Realm, something else?) and access it from both Swift code and React Native.
This is extremely easy with React Native in both iOS and Android with Native Modules.
You would build a simple native module in Swift/Obj-C and expose your persistent store:
//RCTMyDataStore.h
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#interface RCTMyDataStore : RCTEventEmitter <RCTBridgeModule>
#end
and
// RCTMyDataStore.m
#import "RCTMyDataStore.h"
#implementation RCTMyDataStore {
}
RCT_EXPORT_MODULE(MyDataStore);
RCT_REMAP_METHOD(getMyData,
getMyData_resolver:(RCTPromiseResolveBlock)resolve
getMyData_rejecter:(RCTPromiseRejectBlock)reject)
{
// get data from my persistent store
if (success) {
// convert to RN passable format (NSDictionary or NSArray)
resolve(myData);
} else {
reject(#"404", #"No data", nil);
}
}
#end
In your React Native code:
import {
NativeModules
} from 'react-native';
const NativeDataStore = NativeModules.MyDataStore; // must match the RCT_EXPORT_MODULE name
NativeDataStore.getMyData()
.then(data => {
// do some stuff
})
For more info on Native Modules and Swift support, you can check out the docs here: https://facebook.github.io/react-native/docs/native-modules-ios.html#exporting-swift

How to call methods in the custom view in iOS module, from titanium project?

I need to call methods in the custom view in iOS module from titanium project. I have followed the tutorials in Appcelarator documentation about creating iOS module. I could create the custom view in Titanium using below code.
var manage = require('com.test');
var manageView = manage.createView({
left:40,
right:40,
top:40,
height: 250,
backgroundColor:'blue' }); manageView.customMethodInView();
But i got error like "customMethodInView is not a function" when i run the app.
#import "TiViewProxy.h"
#import "CustomView.h"
#interface ComTestViewProxy : TiViewProxy {
CustomView *customView;
}
- (void) customMethodInView;
#end
This is the code in viewProxy class in iOS Module project.
Please help.
I know this is late, but for any wondering soul out there, it is my understanding that even if you don't intend to pass an argument for your method, the signature of the method in the native module should always take one:
your method
- (void) customMethodInView;
should look like this :
- (void)customMethodInView:(id)unused;

How to Implement native module in react-native?

I am following this guide
http://facebook.github.io/react-native/docs/nativemodulesios.html#content
And also this website:
http://colinramsay.co.uk/2015/03/27/react-native-simple-native-module.html
But does not matter where i add the .h and .m files i always get the error:
Class ClassName was not exported Did you forget to use RTC_EXPORT_MODULE()?
Even if it the same code from the examples from react-native documentation, can anyone guide me where to add the .h and .m files and properly link them to the project?
Thanks.
There has been a change in the native module API and it seems the docs haven't been updated accordingly. From the example in my article, SomeString.m should look like this:
// SomeString.m
#import "SomeString.h"
#implementation SomeString
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(get:(RCTResponseSenderBlock)callback)
{
// Change this depending on what you want to retrieve:
NSString* someString = #"something";
callback(#[someString]);
}
#end
This ends up with the desired result and you can call it from JS in the same way as before. It looks like this only just happened:
https://github.com/facebook/react-native/commit/0686b0147c8c8084e4a226b7ea04585362eccea8
You can also just add a plain RCT_EXPORT(); to any method you want to export. Works like a charm.

Method not implemented in protocol (using wit.ai SDK)

I'm using the wit.ai iOS SDK for the first time and I followed step by step what is written in the getting started page in official website https://wit.ai/docs/ios/3.1.1/quickstart. I got this error:
Method 'witDidGraspIntent:entities:body:error:' in protocol 'WitDelegate' not implemented.
I could still run the app and the message is shown in my inbox (in console) but not response is being sent back and the application crashes. I got this error:
Error when enqueuing buffer from callback
Here is my code
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
UILabel *labelView;
}
- (void)witDidGraspIntent:(NSArray *)outcomes
messageId:(NSString *)messageId
customData:(id)customData
error:(NSError*)e {
//Implementation here...
labelView.text = #"Hey what's up";
[self.view addSubview:labelView];
}
ViewController.h
#import <UIKit/UIKit.h>
#import <Wit/Wit.h>
#interface ViewController : UIViewController <WitDelegate>
#end
Thanks.
Dude, the crash message you are getting is telling you exactly what is wrong.
Method 'witDidGraspIntent:entities:body:error:' in protocol
'WitDelegate' not implemented.
You are missing a method (witDidGraspIntent:entities:body:error:) in your implementation of the protocol. You must implement all the required methods in a protocol. In this case the missing method is witDidGraspIntent:entities:body:error:.
You ask "Should I add a new -void ??" By that, if you mean should you add an implementation of the witDidGraspIntent:entities:body:error: method, the answer is YES!
I haven't used the wit.ai SDK before. You might want to edit your question and ask people who have used that SDK for help in implementing the method if you can't figure out how to do it on your own. You might also want to add "(using wit.ai SDK)" to your question title so people familiar with that framework notice your question.

Resources