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(回調){'從中調用嗎?請確保你傳遞了一個有效的回調函數。 –