Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift <-> React Native Bridge duplicate interface

I'm currently trying to make a custom module in Swift and bridge it over to React Native. I've followed the tutorial from the RN docs and even the one commented in their code, but I keep getting the following compiling error:

Duplicate interface definition for class 'StorageManager'

Anyone an idea on how to resolve this error?

The bridge .m file:

#import "RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(StorageManager, NSObject)

RCT_EXTERN_METHOD(getAccessToken:(RCTPromiseResolveBlock*)resolver reject:(RCTPromiseRejectBlock*)reject)
RCT_EXTERN_METHOD(getRefreshToken:(RCTPromiseResolveBlock*)resolver reject:(RCTPromiseRejectBlock*)reject)
RCT_EXTERN_METHOD(getSelectedNetworkId:(RCTPromiseResolveBlock*)resolver reject:(RCTPromiseRejectBlock*)reject)
@end

The swift implementation

import Foundation

@objc(StorageManager)
class StorageManager: NSObject {

    @objc func getAccessToken(resolver:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) {

        let accessToken = Shared.getObjectPrefs("access_token")
        if(accessToken != nil){
            resolver(accessToken)
        }
        else {
            reject("no_access_token", "No access token present", nil)
        }
    }

    @objc func getRefreshToken(resolver:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) {

        let refreshToken = Shared.getObjectPrefs("refresh_token")
        if(refreshToken != nil){
            resolver(refreshToken)
        }
        else {
            reject("no_refresh_token", "No refresh token present", nil)
        }
    }

    @objc func getSelectedNetworkId(resolver:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) {

        let networkId = Shared.getObjectPrefs("current_network_id")
        if(networkId != nil){
            resolver(networkId)
        }
        else {
            reject("no_network_id", "No network id present", nil)
        }
    }


}
like image 926
user1470265 Avatar asked Oct 29 '22 19:10

user1470265


1 Answers

I found solution in Swift 3, RN 0.45.1

in your ReactManager.swift file

import Foundation
import React

@objc(ReactManager) 
class ReactManager: NSObject {
var bridge: RCTBridge!

@objc func addEvent(name: String, location: String, date: NSNumber) -> Void {
    // Date is ready to use!
}

@objc func dismissPresentedViewController(_ reactTag: NSNumber) {
    DispatchQueue.main.async {
        if let view = self.bridge.uiManager.view(forReactTag: reactTag) {
            print(view)
            let presentedViewController = view.reactViewController()
            guard let vc = presentedViewController else { return }
            vc.navigationController?.pop(animated: true)
        }
    }
}
}

in ReactManagerBridge.h file

#import <React/RCTBridgeModule.h>
@interface ReactManagerBridge : NSObject <RCTBridgeModule>
@end

in ReactManagerBridge.m file,

#import "ReactManagerBridge.h"
#import "zigbang-Swift.h"

@implementation ReactManagerBridge

RCT_EXPORT_MODULE(ReactManager);

RCT_EXPORT_METHOD(dismissPresentedViewController:(nonnull NSNumber *)reactTag) {
ReactManager* reactManager = [[ReactManager alloc] init];
[reactManager dismissPresentedViewController:reactTag];

}

RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(nonnull NSNumber *)date)

@end

this works! Hope this answer help you

like image 80
Kyle Yi Avatar answered Nov 02 '22 22:11

Kyle Yi