我在 JS 中看不到任何 iOS 本机模块 [英] I can't see any iOS Native Module in JS
问题描述
我对 React 有一些经验,但我是 React Native 的新手.我已经玩了一段时间了,但是当我尝试为 iOS 编写一个基本的本机模块时我卡住了.我尝试过 Swift 和 Objective C.(我对 Swift 有一些基本的经验,但 Objective C 对我来说是全新的)
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)
一些上下文:
- 该项目是使用
react-native-cli 2.0.1
创建的 - 我使用
XCode 9.1
这里是 .swift 类
Here is the .swift class
import Foundation
@objc(Something)
class Something: NSObject {
@objc
func printSomething() -> Void {
print("Something")
}
}
这是桥文件
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(Something, NSObject)
RCT_EXTERN_METHOD(printSomething)
@end
和 Project-Brigding-Header.h
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>
我的 App.js 文件
My App.js file
...
import { NativeModules } from 'react-native';
...
type Props = {};
export default class App extends Component<Props> {
componentDidMount() {
console.log('NativeModules: ', NativeModules);
}
...
}
问题来了.console.log() 的输出表明 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!
推荐答案
解决方案是记录模块名称 console.log(NativeModules.Something)
,而不是整个 NativeModules 对象.我的设置没有任何问题.
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.
这篇关于我在 JS 中看不到任何 iOS 本机模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!