Last active
January 17, 2024 11:43
-
-
Save robertjpayne/855fdb15d5ceca12f6c5 to your computer and use it in GitHub Desktop.
React Native - Swift Native Modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 }; \ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
@objc(SwiftReactModule) | |
class SwiftReactModule: NSObject { | |
@objc func printMessage(message: String!) { | |
println("SwiftReactModule::printMessage => \(message)") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTSwiftBridgeModule.h" | |
@interface RCT_EXTERN_MODULE(SwiftReactModule, NSObject) | |
RCT_EXTERN_METHOD(printMessage:(NSString *)message) | |
@end |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Objective-C Bridging Header
in the build settings.