Roc*_*ish 6 javascript objective-c ios react-native
但是我注意到每当我从另一个方法调用它时,我的桥变量都是nil.我相信这是因为仅在从javascript调用桥接方法时才设置桥.我已经尝试了从创建委托到创建SingleTon类的所有内容.上述工作都没有,我无法弄清楚为什么它仅在我从Javascript调用的方法中可用.这是我的课
Helper.h
#import "RCTBridge.h"
#import "AppDelegate.h"
#import "RCTEventEmitter.h"
@interface Helper : RCTEventEmitter <RCTBridgeModule>
-(void) auth;
@end
Run Code Online (Sandbox Code Playgroud)
这是我的.m文件:Helper.m
#import "AppDelegate.h"
#import "Helper.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
@implementation Helper
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
- (NSArray<NSString *> *)supportedEvents {
return @[@"SpotifyHelper"];
}
RCT_EXPORT_METHOD(auth)
{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"SpotifyHelper" body:@{@"Login": @true}];
printf("Auth");
}
RCT_EXPORT_METHOD(play:(NSString *) uri first: id)
{
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[appDelegate play:uri second:id];
}
@end
Run Code Online (Sandbox Code Playgroud)
我从我的委托内部调用这个方法,如下所示:
[[AppDelegate alloc] init] auth]
Run Code Online (Sandbox Code Playgroud)
这就是我认为它没有初始化的原因.我不知道如何让RCTBridge变量不为零.有帮助吗?
问题出在这里:
[[AppDelegate alloc] init] auth]
Run Code Online (Sandbox Code Playgroud)
当您使用宏时,RCT_EXPORT_MODULE()
React-Native将为您实例化该类,并且任何后续的alloc/init
s将创建新的实例,与原始实例无关.在这些新实例中不会实例化桥.
您可以使用NSNotifications解决您的问题.
Helper.h:
#import "RCTEventEmitter.h"
@interface Helper : RCTEventEmitter
+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;
@end
Run Code Online (Sandbox Code Playgroud)
Helper.m:
#import "Helper.h"
@implementation Helper
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents {
return @[@"SpotifyHelper"];
}
- (void)startObserving
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(emitEventInternal:)
name:@"event-emitted"
object:nil];
}
- (void)stopObserving
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)emitEventInternal:(NSNotification *)notification
{
[self sendEventWithName:@"SpotifyHelper"
body:notification.userInfo];
}
+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted"
object:self
userInfo:payload];
}
// Remaining methods
@end
Run Code Online (Sandbox Code Playgroud)
这里有一个很长的讨论主题:可以使用什么方法将事件从本机模块发送到JS
只是为了添加 oar.garuna 的答案(这只是一个完整的救星),对于那些有多个事件并想要处理它的人,以下代码将设置一个单一的观察者,然后我们可以对其进行静态调用emitEventWithName
和助手将适当地处理它并将该事件发送出去。
事件助手.h
@interface eventHelper : RCTEventEmitter
+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;
@end
Run Code Online (Sandbox Code Playgroud)
eventHelper.m
@implementation eventHelper
RCT_EXPORT_MODULE();
// The list of available events
- (NSArray<NSString *> *)supportedEvents {
return @[@"EventLoggedOut"];
}
// This function listens for the events we want to send out and will then pass the
// payload over to the emitEventInternal function for sending to Javascript
- (void)startObserving
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(emitEventInternal:)
name:@"event-emitted"
object:nil];
}
// This will stop listening if we require it
- (void)stopObserving
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// This will actually throw the event out to our Javascript
- (void)emitEventInternal:(NSNotification *)notification
{
// We will receive the dictionary here - we now need to extract the name
// and payload and throw the event
NSArray *eventDetails = [notification.userInfo valueForKey:@"detail"];
NSString *eventName = [eventDetails objectAtIndex:0];
NSDictionary *eventData = [eventDetails objectAtIndex:1];
[self sendEventWithName:eventName
body:eventData];
}
// This is our static function that we call from our code
+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload
{
// userInfo requires a dictionary so we wrap out name and payload into an array and stick
// that into the dictionary with a key of 'detail'
NSDictionary *eventDetail = @{@"detail":@[name,payload]};
[[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted"
object:self
userInfo:eventDetail];
}
@end
Run Code Online (Sandbox Code Playgroud)
一旦设置了该部分,您就可以emitEventWithName
从任何地方调用我们的静态函数,如下所示:
[eventHelper emitEventWithName:@"myEventName" andPayload:@{@"key":@"value1"}];
Run Code Online (Sandbox Code Playgroud)
希望对某人有所帮助!
归档时间: |
|
查看次数: |
2344 次 |
最近记录: |