问题
I am building react native using native module. In the javascript side, I want to create native module on iOS for listener event and call some method from javascript side. I have a static NSDictionary variable in Objective C. But when I get value of variable, it show EXC_BAD_ACCESS. I am building react native using native module. In the javascript side, I want to create native module on iOS for listener event and call some method from javascript side. Here is my code
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(){
}
}
In Objective-C, I create: 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
When my app mounted, it call TestMethod.notifyAppMount. Then I push notification, it will set params into resultTemp variable. When I call TestMethod.notifyAppMount() when press button, this code show: EXC_BAD_ACCESS.
+(void)notifyAppMount {
NSDictionary<NSString *, id> *userInfo = resultTemp;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Deeplink"
object:self
userInfo:userInfo];
}
}
In javascriptside, I also can not call DeeplinkManager.getInitialDeeplink() although in deeplink module I declare method. Thank you in advance.
来源:https://stackoverflow.com/questions/56403549/exc-bad-access-when-get-value-of-static-nsstring-varible-in-class-inheritit-rcte