Chr*_*ris 6 javascript swift react-native
我一直在尝试使用Swift构建全面的蓝牙功能.我目前仍然坚持如何将事件从Swift发送回React Native.我试过这个选项,但self.bridge
总是如此nil
.我希望构建一个接口文件,其中包含将我需要的功能桥接到Obj-C所需的所有内容,然后是JS(因为我无法使Bridge模块中的函数监听NSNotification
事件或以其他方式直接调用它们)斯威夫特(我想).
BTInterface.swift
@objc(BTInterface)
class BTInterface: NSObject {
var bridge: RCTBridge!
@objc func shouldScanForPeripheral(shouldScan: Bool) {
log.debug("Should scan: \(shouldScan)")
}
@objc func testEvent(eventName: String) {
// self.bridge is always nil and crashes
self.bridge.eventDispatcher.sendAppEventWithName( eventName, body: "Hello" )
}
}
Run Code Online (Sandbox Code Playgroud)
BluetoothBridge.m
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
@interface RCT_EXTERN_MODULE(BTInterface, NSObject)
RCT_EXTERN_METHOD(shouldScanForPeripheral:(BOOL *)shouldScan);
RCT_EXTERN_METHOD(testEvent:(NSString *)eventName)
@end
Run Code Online (Sandbox Code Playgroud)
那么我想从我的蓝牙逻辑中这样称呼它:
BTInterface().testEvent("TestEvent")
Run Code Online (Sandbox Code Playgroud)
探索其他 一些做类似事情的模块,我尝试添加以下内容,但无济于事:
添加到BTInterface.swift
@objc func initWithBridge(_bridge: RCTBridge) {
self.bridge = _bridge
}
Run Code Online (Sandbox Code Playgroud)