-
-
Save robertjpayne/855fdb15d5ceca12f6c5 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h> | |
#import "RCTBridgeModule.h" | |
#define RCT_EXTERN_MODULE(objc_name, objc_supername) \ | |
RCT_EXTERN_REMAP_MODULE(objc_name, objc_name, objc_supername) | |
#define RCT_EXTERN_REMAP_MODULE(js_name, objc_name, objc_supername) \ | |
objc_name : objc_supername \ | |
@end \ | |
@interface objc_name (RCTExternModule) <RCTBridgeModule> \ | |
@end \ | |
@implementation objc_name (RCTExternModule) \ | |
RCT_EXPORT_MODULE(js_name) | |
#define RCT_EXTERN_METHOD(method) \ | |
RCT_EXTERN_REMAP_METHOD(, method) | |
#define RCT_EXTERN_REMAP_METHOD(js_name, method) \ | |
- (void)__rct_export__##method { \ | |
__attribute__((used, section("__DATA,RCTExport"))) \ | |
__attribute__((__aligned__(1))) \ | |
static const char *__rct_export_entry__[] = { __func__, #method, #js_name }; \ | |
} |
import Foundation | |
@objc(SwiftReactModule) | |
class SwiftReactModule: NSObject { | |
@objc func printMessage(message: String!) { | |
println("SwiftReactModule::printMessage => \(message)") | |
} | |
} |
#import "RCTSwiftBridgeModule.h" | |
@interface RCT_EXTERN_MODULE(SwiftReactModule, NSObject) | |
RCT_EXTERN_METHOD(printMessage:(NSString *)message) | |
@end |
For those who may encounter similar issues to me, you have to update few things in @zxcpoiu example.
In bridging header, you need to add "React/" when import react header files.
#import "React/RCTBridge.h"
#import "React/RCTBridgeModule.h"
#import "React/RCTEventDispatcher.h"
In your native module header (YourModuleBridge.m)
#import "React/RCTBridge.h"
Make sure In Project Build Settings, in Swift Compiler - Code Generation, Objective-C Bridging Header is set to bridging header file path relative to your project.
Pretty sure you already have a bridging header in your swift project, so you wouldn't need to create another one
@robertjpayne @zxcpoiu @oun Do any of you have a working, up-to-date example project that you could share to demonstrate this? I've followed both sets of your instructions, yet I can only get as far as:
Unhandled JS Exception: undefined is not an object (evaluating 'CalendarManager.addEvent')
It seems that my module is not being exposed to JS. I don't really have any stable source code to share as an example (I'm trying different things every few minutes and they're all not working), so really I'd just like to refer to a working example project.
Edit: Looks like I was doing everything right, just running into an undocumented error: you can't name modules by any name that begins with "RCT"!. See my repo at https://github.com/shirakaba/react-native-cfstringtokenizer for an example of an npm-distributable Swift module. Thanks to all your instructions (from 2015-2017) that helped me reach this state!
I am trying to write module in Swift and I tried 2 ways to link it to my main react-native
app:
1st attempt
I tried the documentation in native modules setup but when I try to add new swift code I am getting errors when adding swift code as it written in set up section
2nd attempt
If I follow the guide I steps 1-4 and than tries to link it to my main react-native project than:
- I don't have static library file - which I learned that swift does not support.
- I can't link the project as describe in the example above by @zxcpoiu - I am using Xcode 10 and I don't have
Objective-C Bridging Header
in the build settings.
How can we integrate third party swift packages to the native modules? @robertjpayne Can you explain with example? Is there a need for adding Package.swift or Swift Package Manager would work?
Does this still work?