React Native 0.61.2 iOS端NativeModules.MyNativeModule未定义求助
Hey there, let's troubleshoot why your iOS native module is coming up as undefined—Android working but iOS failing is super common with local RN modules, so let's go through the most likely fixes:
1. Fix the Critical Header Import Error
Looking at your MyNativeModule.h code, you've got a wrong import line:
#import <React/MyNativeModule.h>
This should be importing the RCTBridgeModule header instead, since that's the protocol your module needs to conform to:
#import <React/RCTBridgeModule.h> @interface MyNativeModule : NSObject <RCTBridgeModule> @end
That's a basic but easy-to-miss mistake that would prevent RN from recognizing your module entirely.
2. Verify Pod Integration in Your Main App
Since you're installing the module locally with yarn add ../MyNativeModule, make sure your main React Native app's ios/Podfile includes a reference to the module. Add this line to your Podfile (adjust the path if your node_modules lives in a different location):
pod 'MyNativeModule', :path => '../node_modules/MyNativeModule'
Then run these commands in your app's ios directory to refresh pods:
rm -rf Pods Podfile.lock pod install
Important: Always open your app's .xcworkspace file in Xcode (not the .xcodeproj) when using CocoaPods—this is a frequent source of integration issues.
3. Add a Test Exported Method (Optional but Helpful)
Sometimes React Native might not register a module if it has no exported methods. Add a simple test method to your MyNativeModule.m to ensure the module gets picked up:
#import "MyNativeModule.h" @implementation MyNativeModule RCT_EXPORT_MODULE() // Add this test method to give RN a clear reason to register the module RCT_EXPORT_METHOD(logMessage:(NSString *)message) { NSLog(@"MyNativeModule log: %@", message); } @end
4. Double-Check Your Podspec Configuration
Your podspec looks mostly okay, but let's confirm a couple details:
s.source_files: Your change to"ios/*.{h,m}"is correct if all your native files are directly in theiosfolder (no subfolders).s.dependency 'React': For newer React Native versions, this should work fine, but if you're on RN 0.60+, ensure autolinking is enabled (it's on by default, so this shouldn't be an issue).
5. Perform a Full Clean & Rebuild
Sometimes cached data sticks around even after partial cleans. Follow these steps in order to wipe all stale data:
- Close Metro and Xcode completely
- In your main app directory:
rm -rf node_modules yarn.lock yarn install - In your app's
iosdirectory:rm -rf Pods Podfile.lock pod install - Restart Metro with a cache reset:
npx react-native start --reset-cache - Rebuild your iOS app from Xcode (or run
npx react-native run-ios)
6. Check Xcode Target Configuration (If You Manually Added the Module)
If you ever dragged MyNativeModule.xcodeproj into your main app project manually, make sure:
- The module's target is added to your main app's Target Dependencies (Xcode: your app target > Build Phases > Target Dependencies)
- The
libMyNativeModule.afile is added to Link Binary With Libraries
That said, since you're using a podspec, relying on CocoaPods for integration is better and avoids these manual setup headaches.
Try these steps one by one—most likely the header import fix will resolve the undefined issue, but combining it with a full clean and proper pod integration should get your module working on iOS.
内容的提问来源于stack exchange,提问作者Rishi

