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); 
     } 
    } 
} 

,但得到的失敗消息:

enter image description here

有人能幫助我,爲什麼我有這個問題? 非常感謝!

回答

0

根據上面提供的信息,請嘗試調試,下列選項:

  1. SPReactNativeUtils.js線沒有。 4把控制檯日誌,以檢查是否
  2. 檢查,如果SPNativeiOS定義回調的定義 - 大多數這樣你會更廣闊的畫面如果沒有的話去選擇3
  3. 把一個NSLog的本機代碼的價值。回調b。在不同階段,最後檢查,如果值是越來越賦值給對象
  4. 確保對象運行yarn run ios(創建一個新的構建,只要更改本機代碼)

希望這有助於。如果沒有請提供輸出到上述選項,以幫助我們調試更多:)

+0

1.我試圖打印日誌「undefined」,這就是我問的問題。 2.這就是打印錯誤的原因。 3.從未調用過xcode模塊。 4.我也嘗試過「npm start」,「react-native run-ios」。但仍然不起作用 –

+0

你能指出'靜態pushNotificationEnabled(回調){'從中調用嗎?請確保你傳遞了一個有效的回調函數。 –

相關問題