我正在使用本机模块构建本机反应。在 javascript 方面,我想在 iOS 上为侦听器事件创建本机模块,并从 javascript 端调用一些方法。我在 Objective C 中有一个静态 NSDictionary 变量。但是当我得到变量的值时,它显示 EXC_BAD_ACCESS。我正在使用本机模块构建本机反应。在 javascript 方面,我想在 iOS 上为侦听器事件创建本机模块,并从 javascript 端调用一些方法。这是我的代码
DeepLinkController.js
import React, {Component} from 'react';
import {Platform, Linking, View, NativeModules, NativeEventEmitter} from 'react-native'
const DeeplinkManager = new NativeEventEmitter(NativeModules.DeeplinkManager);
const TestMethod = NativeModules.TestMethod;
class DeepLinkController extends Component{
componentDidMount(){
TestMethod.notifyAppMount();
DeeplinkManager.addListener('DeepLink', this.handleDeepLink);
}
componentWillUnmount(){
TestMethod.notifyAppUnMount();
DeeplinkManager.removeListener('DeepLink', this.handleDeepLink);
}
handleDeepLink = (event) => {
//navigate url
}
render(){
}
}
在 Objective-C 中,我创建:DeeplinkManager.h、DeeplinkManager.m、TestMethod.h、TestMethod.m
DeeplinkManager.h
#if __has_include("RCTEventEmitter.h")
#import "RCTEventEmitter.h"
#else
#import <React/RCTEventEmitter.h>
#endif
NS_ASSUME_NONNULL_BEGIN
static bool isAppMount = false, isNavigateNotitication = false;
static NSMutableDictionary<NSMutableString *, id> *resultTemp;
@interface DeeplinkManager : RCTEventEmitter <RCTBridgeModule>
+ (void)notifyAppMount;
+ (void)notifyAppUnMount;
+ (void)emitEventWithDeeplink:(NSString *)deeplink andPayload:(NSDictionary *)payload;
@end
NS_ASSUME_NONNULL_END
#import "DeeplinkManager.h"
@implementation DeeplinkManager
RCT_EXPORT_MODULE();
- (void)startObserving
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendDeeplink:)
name:@"Deeplink"
object:nil];
}
- (void)stopObserving
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)sendDeeplink:(NSNotification *)notifcation {
NSDictionary<NSString *, id> *payload = @{@"deeplink": notifcation.userInfo[@"deeplink"],@"userInfo":notifcation.userInfo[@"userInfo"]};
[self sendEventWithName:@"Deeplink" body:payload];
}
+ (void)emitEventWithDeeplink:(NSString *)deeplink andPayload:(NSDictionary *)payload {
isNavigateNotitication = true;
resultTemp = @{@"deeplink": deeplink,@"userInfo":payload};
}
RCT_EXPORT_METHOD(getInitialDeeplink:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
//some code for handle get initial deeplink
}
+(void)notifyAppMount {
NSDictionary<NSString *, id> *userInfo = resultTemp;
[[NSNotificationCenter defaultCenter] postNotificationName:@"SMTDeeplink"
object:self
userInfo:userInfo];
}
}
+(void)notifyAppUnMount {
isAppMount = false;
}
@end
---TestMethod.h
#import <React/RCTBridgeModule.h>
@interface TestMethod : NSObject <RCTBridgeModule>
@end
--- TestMethod.m
#import "TestMethod.h"
#import "DeeplinkManager.h"
@implementation TestMethod
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(notifyAppMount)
{
[DeeplinkManager notifyAppMount];
}
RCT_EXPORT_METHOD(notifyAppUnMount)
{
[DeeplinkManager notifyAppUnMount];
}
@end
当我的应用程序挂载时,它会调用 TestMethod.notifyAppMount。然后我推送通知,它会将参数设置为 resultTemp 变量。当我按下按钮时调用 TestMethod.notifyAppMount() 时,此代码显示:EXC_BAD_ACCESS。
+(void)notifyAppMount {
NSDictionary<NSString *, id> *userInfo = resultTemp;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Deeplink"
object:self
userInfo:userInfo];
}
}
在 javascriptside 中,我也不能调用 DeeplinkManager.getInitialDeeplink() 尽管在 deeplink 模块中我声明了方法。先感谢您。