简体   繁体   English

在react-native模块的podspec中反应依赖

[英]React dependency in react-native module's podspec

I'm looking at source code of Microsoft's Mobile center SDK for react-native. 我正在查看微软移动中心SDK的源代码 ,用于本地反应。 There are several native modules for react-native, and the recommended way to install them is via Cocoapods. 有几个原生模块用于react-native,推荐的安装方法是通过Cocoapods。

Now, each module has podspec, but none mention React as dependency. 现在,每个模块都有podspec,但没有一个提到React作为依赖。 For example, appcenter-crashes.podspec has these dependencies: 例如, appcenter-crashes.podspec具有以下依赖项:

s.dependency 'AppCenterReactNativeShared'
s.dependency 'AppCenter/Crashes' 

And AppCenterReactNativeShared.podspec has just AppCenterReactNativeShared.podspec就是

s.dependency 'AppCenter/Core', '1.1.0'

Yet the module are react-native bridges and depend on React and import from React in their sources. 然而,该模块是反应原生的桥梁,依赖于React并从其来源从React导入。 For example, AppCenterReactNativeCrashes.m has these lines: 例如, AppCenterReactNativeCrashes.m包含以下行:

#import "AppCenterReactNativeCrashes.h"

// Support React Native headers both in the React namespace, where they are in RN version 0.40+,
// and no namespace, for older versions of React Native
#if __has_include(<React/RCTAssert.h>)
#import <React/RCTAssert.h>
#import <React/RCTBridgeModule.h>
...

Next, in my react-native project I can link mobile center and build and run my app with Podfile like this: 接下来,在我的react-native项目中,我可以链接移动中心,使用Podfile构建和运行我的应用程序,如下所示:

target 'example' do
  pod 'AppCenter/Crashes', '~> 1.0.1'
  pod 'AppCenter/Analytics', '~> 1.0.1'
  pod 'AppCenterReactNativeShared', '~> 1.0.1'
end

Again, I don't need to mention React pod and its subspecs here! 同样,我不需要在这里提到React pod及其子规格!

Now in my own react-native native module I have to declare dependencies like: 现在在我自己的react-native本机模块中,我必须声明依赖项,如:

s.dependency 'React'
s.dependency 'some-other-dependency-my-module-needs'

And then in my app's Podfile I have include React pod: 然后在我的应用程序的Podfile我包含了React pod:

pod 'yoga', path: "#{rn_path}/ReactCommon/yoga/yoga.podspec"
pod 'React', path: rn_path, subspecs: [
  'Core',
  'RCTActionSheet',
...

If I don't include React in native module podspec then I get error fatal error: 'React/RCTBridgeModule.h' file not found . 如果我没有在本机模块podspec中包含React ,那么我得到错误fatal error: 'React/RCTBridgeModule.h' file not found If I don't add React to Pofile, then Cocoapods drags deprecated React pod from its repository and the build breaks. 如果我没有将React添加到Pofile,那么Cocoapods drags会从其存储库中弃用React pod并且构建中断。

So, my question is how is it possible that mobile center native modules do not have React in their podspecs and how can I get rid of it in my native module? 所以,我的问题是移动中心本机模块如何在其podspec中没有React ,如何在我的本机模块中摆脱它?

As per the App Center SDK documentation for React Native , you have to run react-native link which will add the native dependencies for React Native for iOS. 根据React NativeApp Center SDK文档 ,您必须运行react-native link ,该react-native link将为React Native for iOS添加本机依赖项。

The react-native version is actually defined in package.json at Javascript level and react-native link will install the corresponding native dependencies for that version. react-native版本实际上是在Javascript级别的package.json中定义的, react-native link将为该版本安装相应的本机依赖项。

Also when we make changes to the SDK, we use a test app to host the project so we can edit both test application and SDK code in the same XCode workspace. 此外,当我们对SDK进行更改时,我们使用测试应用程序来托管项目,以便我们可以在同一个XCode工作区中编辑测试应用程序和SDK代码。

Let me explain how "greenfield" (react native app created from scratch) and "brownfield" (existing Objective-C/Swift app migrated to react native) iOS apps work for App Center React Native SDK . 让我解释一下“greenfield”(从头开始创建本机应用程序)和“brownfield”(现有的Objective-C / Swift应用程序迁移到本机反应)iOS应用程序如何适用于App Center React Native SDK

For "greenfield" React Native app created from scratch, whether it is created via react-native init {myapp} or via create-react-native-app {myapp} , react and react-native dependencies are added automatic during the creation and referenced in your package.json . 对于从头开始创建的“绿地”React Native应用程序,无论是通过react-native init {myapp}还是通过create-react-native-app {myapp}创建的,在创建和引用期间会自动添加reactreact-native依赖项。在你的package.json

For "brownfield" React Native app, assuming you followed the React Native Integration with Existing Apps documentation to convert your Objective-C/Swift app into React Native, you need to reference React dependencies in your Podfile like the example in Configuring CocoaPods dependencies : 对于“brownfield”React Native应用程序,假设您遵循React Native Integration with Existing Apps文档将Objective-C / Swift应用程序转换为React Native,您需要在Podfile引用React依赖项,如配置CocoaPods依赖项中的示例:

# The target name is most likely the name of your project.
target 'NumberTileGame' do

  # Your 'node_modules' directory is probably in the root of your project,
  # but if not, adjust the `:path` accordingly
  pod 'React', :path => '../node_modules/react-native', :subspecs => [
    'Core',
    'DevSupport', # Include this to enable In-App Devmenu if RN >= 0.43
    'RCTText',
    'RCTNetwork',
    'RCTWebSocket', # needed for debugging
    # Add any other subspecs you want to use in your project
  ]
  # Explicitly include Yoga if you are using RN >= 0.42.0
  pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

  # Third party deps podspec link
  pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  pod 'GLog', :podspec => '../node_modules/react-native/third-party-podspecs/GLog.podspec'
  pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'

end

You can take a look at the Podfile of the sample "brownfield" app in App Center SDK. 您可以在App Center SDK中查看示例“ Podfile ”应用程序的Podfile In this example, React is reference in the Podfile and it is required for the brownfield app to find react references. 在此示例中, ReactPodfile引用,并且棕色区域应用程序需要查找反应引用。 In other words, without React reference in Podfile , you will see errors similar to fatal error: 'React/RCTBridgeModule.h' file not found . 换句话说,如果没有Podfile React引用,您将看到类似于fatal error: 'React/RCTBridgeModule.h' file not found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM