I am a novice in Swift and IOS right now. I am making a React Native app that uses IOS native code written in Swift. These methods in order to be linked to React native need an implementation file. I have already patched few methods from my Swift class which needs to subclass NSObject. But now I need to patch events also from there and Swift won't allow me to subclass multiple classes.
Here is the Swift file
@objc func initializeCoreLocation(_ callback: @escaping RCTResponseSenderBlock) {
locationManager.delegate = self
locationManager.allowsBackgroundLocationUpdates = true
callback(["initialize Location"])
}
@objc func initializeNotifications(_ callback: @escaping RCTResponseSenderBlock) {
let center = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.alert, .sound]
center.requestAuthorization(options: options) {
(granted, error) in
if !granted {
NSLog("Permission not granted for notifications");
callback(["Notification permission not granted"])
} else if (granted) {
callback(["Notificaiton permission granted"])
}
}
}
In order to make these methods accessible to React Native I had to add an implementation file
@interface RCT_EXTERN_MODULE(BeaconManager, NSObject)
RCT_EXTERN_METHOD(initializeCoreLocation:
(RCTResponseSenderBlock)callback);
RCT_EXTERN_METHOD(checkAuthorization:
(RCTResponseSenderBlock)callback);
RCT_EXTERN_METHOD(initializeNotifications: (RCTResponseSenderBlock)callback);
RCT_EXTERN_METHOD(initializeMonitoring: (RCTResponseSenderBlock)callback);
Now I need to patch 2 events in this Swift file along with these methods to React Native
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
// logMessage("didEnterRegion \(region.identifier)")
NSLog("Found a beacon %d", region.identifier)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
//logMessage("didExitRegion \(region.identifier)")
NSLog("Lost a beacon %d", region.identifier)
}
@end
In order to do that I need to subclass RCTEventEmitter too in the implementation file, but Swift won't allow something like this.
@interface RCT_EXTERN_MODULE(BeaconManager, NSObject, RCTEventEmitter)
How should I solve this? Do I need to create another class and patch that class here? Or is there a way to subclass more than one in the implementation file.