My custom native module is not present inside NativeModules object - ios

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

Related

Receiver '**' for class message is a forward declaration Error. Swift Static Library use in Objective-C

I am trying to make a Swift Static library and apply it to Swift and Objective Project.
import Foundation
#objc open class Library001_Test: NSObject {
public override init(){}
#objc public func testPrint() {
print("My Name is Andi")
}
#objc public func getUUID(userName: String) -> String {
let uuid = UUID().uuidString
return "\(userName)'s UUID : \(uuid)"
}
}
I wrote the code like this using Swift.
And in the Edit Scheme menu, I changed the Build Configuration to Release and proceeded with Run. As a result, the 'libLibrary001.a' file and the 'Library001.swiftmodule' folder were created.
These two artifacts work well when pasted into a Swift project and imported.
But the problem is an Objective-C project.
I put both artifacts into my project and checked:
[General - Frameworks, Libraries. and Embedded Content] whether the library is recognized
Whether the library is recognized in [Build Phases - Link Binary With Libraries]
Check [Build Settings - Library Search Paths] address
Defines Module - Yes
And I put '#class Library001_Test;' in ViewController.h
#import <UIKit/UIKit.h>
#class Library001_Test;
#interface ViewController : UIViewController
#end
And in ViewController.m, '#import "ProductName-Swift.h" and the created Class were loaded.
#import "ViewController.h"
#import "SwiftInObjectiveC-Swift.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
Library001_Test *test = [[Library001_Test alloc] init];
}
#end
error : Receiver 'Library001_Test' for class message is a forward declaration
error : Receiver type 'Library001_Test' for instance message is a forward declaration
An error occurred while doing this. I've tried all the methods I've found on the internet and I'm wondering where the problem is.
Is the code the problem? Did I not set it up well??
The Swift file created in the project is import well in Objective-C... Why the hell is the .a file not working like this?
My problem was with '(ProductName)-Swift.h'
If you look at how Swift Code is used in Objective-C, many articles say to import (ProductName)-Swift.h. So I only added the project header that I want to apply, but I also need to add the product header made from the library.
My problem was simple, but it took me a long time to figure it out. The error was not found 'Class' and 'func' in Swift static library. My workaround was resolved using the (LibraryProductName)-Swift.h of the library I created, rather than the (ProductName)-Swift.h of the project you are working on.
If you refer to the address below, you can prevent the error that occurred in advance.
https://medium.com/#mail2ashislaha/swift-objective-c-interoperability-static-libraries-modulemap-etc-39caa77ce1fc

Importing Swift Class into Obj-C in React Native module

I am developing a new feature for a react native module (https://github.com/blackuy/react-native-twilio-video-webrtc) the thing is it is developed in Objective-C and I need to import a new Swift class.
What I've done so far:
Importing it with the name of the package, in this case #import <RNTwilioVideoWebRTC/RNTwilioVideoWebRTC-Swift.h>
Ensuring that Precompile Bridging Header is set to Yes
Setting Objective-C Generated Interface Header Name as RNTwilioVideoWebRTC-Swift.h
The problem is then i try to run the example app inside the module, it prompts an error RNTwilioVideoWebRTC/RNTwilioVideoWebRTC-Swift.h file not found
What am I missing? Thanks!
There are a few things to make sure that:
Check whether the bridging file is available in your project or not. If available, make sure that you have imported YourProductName-Swift.h in your bridge file. And if not, please create a bridge file.
Mark the class & methods with #objc to make them accessible in your Objective C code.
You can follow this answer for reference.
Archiving will be successful already tested
How to call a swift function (that could be in a swift framework also) in objective c project or react native project
it will work for react-native also
follow these steps :
Open Build Settings and check these parameters:
Defines Module : YES
in AppDelegate.h
#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#class KB;
#interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
#property (strong, nonatomic) KB *appkb;
#end
in AppDelegate.m
#import "AppDelegate.h"
#import "ProductModuleName-Swift.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.appkb methodReceiveSwiftClass];
[self.appkb methodEvents:event prop:properties];
}
#end
KB.swift
import Foundation
// import framework if using a swift framework in objective c or react native native modules.
#objc public class KB:NSObject{
#objc public func methodReceiveSwiftClass(){
//write anything here....
}
#objc public func methodEvents(_ event: String, prop: String){
//write anything here
//func with params
}
}
ProjectModuleName-Bridging-Header.h
#import "React/RCTBridgeModule.h"
#import "AppDelegate.h"

React Native App Stuck On Splash Screen

I am trying to learn how to build a react-native module. I followed a tutorial and I am getting stuck on the splash screen. I also get this error when I try to run my app:
Thread 1: signal SIGABRT
I have not modified any of the code in index.ios.js but I have created and two files:
GifMaker.h
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"
#interface GifMaker : NSObject
#end
GifMaker.m
#import "GifMaker.h"
#import "React/RCTLog.h"
#implementation GifMaker
// This RCT (React) "macro" exposes the current module to JavaScript
RCT_EXPORT_MODULE();
// We must explicitly expose methods otherwise JavaScript can't access anything
RCT_EXPORT_METHOD(get)
{
RCTLogInfo(#"Hello There!");
}
#end
I found a solution, I used react-native-create-library to create the .h and .m files and then just copied those into my project.

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.

Unknown type name when trying to include a library

I'm trying to use Novocaine in an iPhone application I'm building. I can't figure out how to get around this error I'm getting:
Unknown type name 'RingBuffer'
Here's my file structure:
...with those files under Novocaine being the ones pulled from the Github repo for Novocaine. Here's my header file for DDViewController.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "UICoordButton.h"
#import "Novocaine.h"
#import "RingBuffer.h"
#import "AudioFileReader.h"
#import "AudioFileWriter.h"
#interface DDViewController : UIViewController
{
RingBuffer *ringBuffer;
Novocaine *audioManager;
AudioFileReader *fileReader;
AudioFileWriter *fileWriter;
}
- (IBAction)changeColor:(id)sender;
- (IBAction)changeColor2:(id)sender;
#end
I've tried a solution that I found on another question, which suggests that this should work:
#class RingBuffer;
#interface DDViewController : UIViewController
{
...
But that just gives me Redefinition of 'RingBuffer' as a different kind of symbol.
How can I fix this problem and use RingBuffer?
RingBuffer is a C++ class. I recommend you change the extension of your Objective-C files from .m to .mm which will make them Objective-C++
Found the answer (or I guess a combination of answers):
Follow coryalder's advice for setting the compiler default to Objective-C++ as described here.
Also, change all .m files to .mm files, and finally add -fno-objc-arc compiler flags to all the .mm files -- which is described in detail here.

Resources