斯威夫特< - > React Native Bridge复制接口

时间:2016-09-23 13:12:31

标签: swift xcode react-native

我目前正在尝试在Swift中创建一个自定义模块并将其桥接到React Native。我已经按照RN docs的教程,甚至是他们的代码中的评论,但我不断收到以下编译错误:

Duplicate interface definition for class 'StorageManager'

有关如何解决此错误的任何想法?

网桥.m文件:

#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

快速实施

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)
        }
    }


}

1 个答案:

答案 0 :(得分:2)

我在Swift 3中找到了解决方案,RN 0.45.1

在您的CTSchemeColor文件中

ReactManager.swift
<{1>}文件中的

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)
        }
    }
}
}

ReactManagerBridge.h文件中,

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

这个有效! 希望这个答案可以帮助你