Writing a Legacy Native Module in Swift
Create an Objective-C bridge file and a Swift class that conforms to RCTBridgeModule, expose a method with RCT_EXPORT_METHOD, and call it from React Native JS.
iOS Native Modules Overview
On iOS, legacy native modules expose Swift or Objective-C code to JavaScript through an Objective-C bridge. Even when you write the implementation in Swift, React Native requires an Objective-C bridge header file to register the module because the RN bridge is built in Objective-C. You write a thin .m bridge file alongside your Swift class.
Creating the Swift Module Class
Create a Swift class that conforms to NSObject and is decorated with the @objc attribute so Objective-C can see it. The class implements the RCTBridgeModule protocol. Swift modules also need @objcMembers on the class so all methods are visible to the bridge without individual @objc annotations on each one.
// CalendarModule.swift
import Foundation
@objc(CalendarModule)
class CalendarModule: NSObject {
@objc
func createCalendarEvent(
_ name: String,
location: String
) {
print('Creating event: \(name) at \(location)')
}
}All lessons in this course
- Writing a Legacy Native Module in Kotlin
- Writing a Legacy Native Module in Swift
- Turbo Native Modules with JSI
- Async Callbacks, Promises, and Events from Native