我已經創建了下面的類(精簡版),以JS,繼承人蔘考完整的文件 https://github.com/cotyembry/CastRemoteNative/blob/7e74dbc56f037cc61241f6ece24a94d8c52abb32/root/ios/CastRemoteNative/NativeMethods.swift發送事件從SWIFT或Objective-C的
@objc(NativeMethods)
class NativeMethods: RCTEventEmitter {
@objc(sendEventToJSFromJS)
func sendEventToJSFromJS {
self.emitEvent(eventName: "test", body: "bodyTestString")
}
func emitEvent(eventName: String: body: Any) {
self.sendEvent(withName: eventName, body: body)
}
}
這完美的作品,並觸發我的回調監聽器是在我的javascript代碼時,我調用emitEvent
方法類似下面,它從 https://github.com/cotyembry/CastRemoteNative/blob/7e74dbc56f037cc61241f6ece24a94d8c52abb32/root/js/Components/ChromecastDevicesModal.js
改變片段從JavaScript端
import {
NativeModules,
NativeEventEmitter
} from 'react-native'
//here I bring in the swift class to use inside javascript
var NativeMethods = NativeModules.NativeMethods;
//create an event emitter to use to listen for the native events when they occur
this.eventEmitter = new NativeEventEmitter(NativeMethods);
//listen for the event once it sends
this.subscription = this.eventEmitter.addListener('test', (body) => { console.log('in test event listener callback', body)});
NativeMethods.sendEventToJSFromJS() //call the native method written in swift
我只是有sendEventToJSFromJS
方法調用上的按鈕按下的JavaScript
同樣,這個工作和console.log('in test event listener callback', body)
代碼工作和運行在JavaScript端
我的問題,即這不工作:
如果我定義的類後做迅速文件中的以下,這是行不通的:
var nativeMethodsInstance = nativeMethods()
nativeMethodsInstance.sendEventToJSFromSwift()
爲什麼?因爲下面的錯誤被拋出:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'bridge is not set. This is probably because you've explicitly synthesized the bridge in NativeMethods, even though it's inherited from RCTEventEmitter.'
因此,創建NativeMethods的instance
時,對不......有什麼區別?
有關其他信息:
Objective-C中得到相同的橋樑沒有設置問題,當我寫的代碼,這些相同的片段中的.h和.m文件,而不是在.swift文件
我發現這裏是越來越打印錯誤消息中的本地代碼,但它只是具有可變
_bridge
並且被檢查以查看它是否是nil
的文件是這個錯誤來自是:
RCTEventEmitter.h
RCTEventEmitter.c
這裏是RCTEventEmitter.c
- (void)sendEventWithName:(NSString *)eventName body:(id)body
{
RCTAssert(_bridge != nil, @"bridge is not set. This is probably because you've "
"explicitly synthesized the bridge in %@, even though it's inherited "
"from RCTEventEmitter.", [self class]);
if (RCT_DEBUG && ![[self supportedEvents] containsObject:eventName]) {
RCTLogError(@"`%@` is not a supported event type for %@. Supported events are: `%@`",
eventName, [self class], [[self supportedEvents] componentsJoinedByString:@"`, `"]);
}
if (_listenerCount > 0) {
[_bridge enqueueJSCall:@"RCTDeviceEventEmitter"
method:@"emit"
args:body ? @[eventName, body] : @[eventName]
completion:NULL];
} else {
RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName);
}
}
充分片斷哪裏該_bridge值被置以及它是怎樣到達集,所以我可以知道,在它發生故障的情況下,如何設置
我發現下面還RCTEventEmitter.h
@property (nonatomic, weak) RCTBridge *bridge;
在給出的錯誤中提到該橋是在RCTEventEmitter中繼承的,所以這可能是weak
部分與bridge
屬性的問題?
或者我需要改變我的策略,我如何在一起做這件事?
我知道它可能有要的東西都與我沒有完全理解代碼的
@synthesize bridge = _bridge;
一部分,並在犯規幫助混合所有的語言多笑...
這真的很難,所以任何幫助將非常感謝! 非常感謝您的時間
這裏是當項目歷史碼代表我上面的問題的代碼整個項目的鏈接(因爲我自做變更項目):
https://github.com/cotyembry/CastRemoteNative/tree/7e74dbc56f037cc61241f6ece24a94d8c52abb32
爲什麼你會混合使用SWIFT代碼一個Objective-C的appDelegate? –
,因爲我需要它將代碼 –
中的rootView.bridge(即網橋變量)@thibautnoah暴露給其他部分,您可以通過修改我在AppDelegate.m中使用的代碼來使用.swift版本的應用程序委託,但從我記憶中,在Swift中,它用我們使用的方法來給我提供那種語言的問題,以便讓我獲得橋接(但是從我看過它已經有一段時間了) –