How to Implement native module in react-native? - ios

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.

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) {
}
}

Use of undeclared type 'PKCS7' (Receipt-Validation/OpenSSL)

To set up Receipt-Validation in my iOS app, I am now following this tutorial:
https://www.raywenderlich.com/9257-in-app-purchases-receipt-validation-tutorial
and reading at this point: Loading the Receipt.
While reading and attempting to understand what is going on I also try to integrate the code in my own app, by doing so getting a hands-on understanding of the process.
Here is one problem I am hitting at this moment:
On this line of code:
private func loadReceipt() -> UnsafeMutablePointer<PKCS7>? {
I get this error message:
Use of undeclared type 'PKCS7'
After searching the net and trying a few things, I guess it is related to the use of the use of the ReceiptVerifier-Bridging-Header.h file. But I am not sure how to set it in the project.
I will be glad if anyone has some tip allowing me to move forward.
Thanks in advance!
In case this can be useful, here is the meaningful contents of the bridging header file (ReceiptVerifier-Bridging-Header.h):
#import <openssl/pkcs7.h>
#import <openssl/objects.h>
#import <openssl/evp.h>
#import <openssl/ssl.h>
#import <openssl/asn1_locl.h>
The problem is that I had simply copied the bridge-header file, without doing the proper setting as explained here:
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift

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

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.

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;

Header file issue with xcode 7

I have a subclass of NSObject, but I have a problem with header file, when I run the project, it shows me this:
and it allows me to add this, but directly this logical error is returned: "#end must appear in Objective-C context"
I precise that with Xcode6 I didn't have that issue with this Does anyone knows how to fix this frustrating issue ?
EDIT:
that's a constants file, do not pay attention to the file's name, here is .m file:
#import "CUSBoxes.h"
const int defaultCount = 10;
const long int repeat = 25000;
const NSString *defaultDescription = #"If any layout issue is related, change default values of each box you want to display";
#implementation CUSBoxes
#end
Usually this error causes you missed #end or invalid character in one of the .h file you used in your project. I have came across with this error, this error not exist in same files it throws check for other header files to fix this issue.
1) You might missing #end in any of the Header file.Check one by one all .h file which has #interface section,should be ended with "#end".
2) If everything looks good then Try cleaning project or Restart Xcode.(In my case,It worked).

Resources