0
我正在实现本机接口来访问iOS功能。React Native iOS本机模块未定义?
//SPRNUtilities.h at /ios/project_name
#import <React/RCTBridgeModule.h>
@interface SPRNUtilities : NSObject <RCTBridgeModule>
@end
//SPRNUtilities.m
#import "SPRNUtilities.h"
@implementation SPRNUtilities
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(pushNotificationEnabled:(RCTResponseSenderBlock)callback)
{
id object = nil;
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]){
object = @(YES);
}else{
object = [NSNull null];
}
callback(@[object]);
}
当我访问它的JS代码,
import { Platform, StyleSheet, NativeModules} from 'react-native';
export default class SPReactNativeUtils{
static pushNotificationEnabled(callback){
if (Platform.OS === 'ios'){
const SPNativeiOS = NativeModules.SPRNUtilities;
SPNativeiOS.pushNotificationEnabled((enabled) => {
callback(enabled);
});
}else{
callback(false);
}
}
}
,但得到的失败消息:
有人能帮助我,为什么我有这个问题? 非常感谢!
1.我试图打印日志“undefined”,这就是我问的问题。 2.这就是打印错误的原因。 3.从未调用过xcode模块。 4.我也尝试过“npm start”,“react-native run-ios”。但仍然不起作用 –
你能指出'静态pushNotificationEnabled(回调){'从中调用吗?请确保你传递了一个有效的回调函数。 –