Skip to content

Commit bfdd1d6

Browse files
committed
Merge pull request #1021 from robertjpayne/master
Documentation for exporting Swift Native Modules
2 parents abea586 + 8c8b289 commit bfdd1d6

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

docs/NativeModulesIOS.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ permalink: docs/nativemodulesios.html
77
next: nativecomponentsios
88
---
99

10-
Sometimes an app needs access to platform API, and React Native doesn't have a corresponding module yet. Maybe you want to reuse some existing Objective-C or C++ code without having to reimplement it in JavaScript, or write some high performance, multi-threaded code such as for image processing, a database, or any number of advanced extensions.
10+
Sometimes an app needs access to platform API, and React Native doesn't have a corresponding module yet. Maybe you want to reuse some existing Objective-C, Swift or C++ code without having to reimplement it in JavaScript, or write some high performance, multi-threaded code such as for image processing, a database, or any number of advanced extensions.
1111

1212
We designed React Native such that it is possible for you to write real native code and have access to the full power of the platform. This is a more advanced feature and we don't expect it to be part of the usual development process, however it is essential that it exists. If React Native doesn't support a native feature that you need, you should be able to build it yourself.
1313

14-
This is a more advanced guide that shows how to build a native module. It assumes the reader knows Objective-C (Swift is not supported yet) and core libraries (Foundation, UIKit).
14+
This is a more advanced guide that shows how to build a native module. It assumes the reader knows Objective-C or Swift and core libraries (Foundation, UIKit).
1515

1616
## iOS Calendar Module Example
1717

@@ -265,3 +265,39 @@ var subscription = DeviceEventEmitter.addListener(
265265
subscription.remove();
266266
```
267267
For more examples of sending events to JavaScript, see [`RCTLocationObserver`](https://github.com/facebook/react-native/blob/master/Libraries/Geolocation/RCTLocationObserver.m).
268+
269+
## Exporting Swift
270+
271+
Swift doesn't have support for macros so exposing it to React Native requires a bit more setup but works relatively the same.
272+
273+
Let's say we have the same `CalendarManager` but as a Swift class:
274+
275+
```swift
276+
// CalendarManager.swift
277+
278+
@objc(CalendarManager)
279+
class CalendarManager: NSObject {
280+
281+
@objc func addEvent(name: String, location: String, date: NSNumber) -> Void {
282+
// Date is ready to use!
283+
}
284+
285+
}
286+
```
287+
288+
> **NOTE** It is important to use the @objc modifiers to ensure the class and functions are exported properly to the Objective-C runtime.
289+
290+
Then create a private implementation file that will register the required information with the React Native bridge:
291+
292+
```objc
293+
// CalendarManagerBridge.m
294+
#import "RCTBridgeModule.h"
295+
296+
@interface RCT_EXTERN_MODULE(CalendarManager, NSObject)
297+
298+
RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(NSNumber *)date)
299+
300+
@end
301+
```
302+
303+
You can also use `RCT_EXTERN_REMAP_MODULE` and `RCT_EXTERN_REMAP_METHOD` to alter the JavaScript name of the module or methods you are exporting. For more information see [`RCTBridgeModule`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridgeModule.h).

0 commit comments

Comments
 (0)