I can't see any iOS Native Module in JS

白昼怎懂夜的黑 提交于 2020-01-14 18:44:10

问题


I have some experience with React but I'm new to React Native. I've played around for a while, but I got stuck when I tried to write a basic native module for iOS. I've tried with both Swift and Objective C. (I have some basic experience with Swift, but Objective C is completely new for me)

Some context:

  1. The project is created with react-native-cli 2.0.1
  2. I use XCode 9.1

Here is the .swift class

import Foundation

@objc(Something)
class Something: NSObject {

  @objc
  func printSomething() -> Void {
    print("Something")
  }
}

Here is the bridge file

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(Something, NSObject)

RCT_EXTERN_METHOD(printSomething)

@end

And the Project-Brigding-Header.h

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import <React/RCTBridgeModule.h>

My App.js file

...
import { NativeModules } from 'react-native';
...
type Props = {};
export default class App extends Component<Props> {
  componentDidMount() {
    console.log('NativeModules: ', NativeModules);
  }
...
}

Here is the problem. The output of console.log() says NativeModules is an empty object:

2018-02-22 18:19:04.590 [info][tid:com.facebook.react.JavaScript] 'NativeModules', {}
2018-02-22 18:19:04.589970+0200 velimo_app_rn[14748:400982] 'NativeModules', {}

I don't understand what I'm doing wrong. I've read pretty much everything I could find only related to the topic but I can't see what I do wrong. If you have any suggestion, please let me know. Thanks!


回答1:


The solution was to log the module name console.log(NativeModules.Something), not the whole NativeModules object. It wasn't anything wrong with my setup.



来源:https://stackoverflow.com/questions/48932408/i-cant-see-any-ios-native-module-in-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!