如何添加第三方SDK(多个.framework文件)来反应本机库模块?

sbs*_*ter 3 ios swift react-native swift-framework react-native-native-module

我已经构建了一个 React Native 库模块(使用 RN 0.63)。该模块依赖于一些第三方SDK。当与 Android 集成(使用 .aar 文件)时,它工作得很好。对于 iOS,我已经能够在没有 SDK 的情况下让库模块工作(使用 swift 因此带有桥接头)。添加 SDK 时,我收到诸如 .h is not available 之类的错误。

这是我的目录我的目录结构:

react-native-lib
--android
--ios
----MyCls.swift
----MyCls.m
----react-native-lib-Bridging-Header.h
----SDKS
------DEBUG
--------A.framework
--------B.framework
--------A-Debug.podspec
--------B-Debug.podspec
------THIRDPARTY
--------JSONModel.framework
--------CocoaLumberjack.framework
--------... other frameworks
--react-native-lib.podspec
--Example
--index.js
--Logger.swift
--package.json
Run Code Online (Sandbox Code Playgroud)

我有一个 Swift 示例应用程序,它使用 SDKS 文件夹,但我似乎无法让 RN 识别框架文件/标头。react-native-lib的Podspec文件最后几行如下:

  ...
  s.dependency "React"
  s.dependency 'JSONModel', '~> 1.8.0'
  s.dependency 'CocoaLumberjack', '~> 3.6.1'
Run Code Online (Sandbox Code Playgroud)

我的示例应用程序 Podfile:

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '11.0'
use_frameworks!

project 'example', {
    'Debug' => :debug,
    'Release' => :release,
}
#
def applibs
 pod 'A-Debug', :configuration => ['Debug'], :path  => '../node_modules/react-native-lib/ios/SDKS/DEBUG/A-Debug.podspec'
# ... A-Release, B-Debug, B-Release
# The release folders not shown in structure above.
end



target 'example' do
 config = use_native_modules!

 use_react_native!(:path => config["reactNativePath"])
  
   applibs
  
  # Disabled Flipper because of use_frameworks!
  
end
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么以及如何克服这个问题。关于如何将此类第三方 SDK 集成到库模块中的文章似乎并不多。我探索过类似的问题,比如这个问题,但仍未解决且信息不足。

sbs*_*ter 5

经过几天的研究和实验,我已经能够解决这个问题。这很简单,但由于缺乏该主题的资源而变得困难。

首先,我使用了 React Native lib(ios 文件夹)中的 podspec 文件来添加对 3rd 方框架的依赖项,如下所示。

反应本机lib.podspec

  s.dependency 'A-Debug', '~> 1.2.3', :configurations => :debug
  s.dependency 'B-Debug', '~> 2.3.4', :configurations => :debug
  s.dependency 'A-Release', '~> 1.2.3', :configurations => :release
  s.dependency 'B-Release', '~> 2.3.4', :configurations => :release
Run Code Online (Sandbox Code Playgroud)

在我的示例应用程序中,podfile 的工作方式如上所示(通过在 applibs 中添加 pod)。但是,我遇到了“启用位码”错误,编译器要求我重新编译启用位码的第 3 方库。我使用应用程序(不是库)podfile 中的以下安装后脚本(从此处获取)解决了这个问题。

示例/ios/Podfile

  post_install do |installer|
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['ENABLE_BITCODE'] = 'NO'
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

进行缓存清理,并作为额外措施,清除节点模块。然后只需在应用程序目录中运行以下命令:

yarn install && cd ios && pod install && cd .. && yarn react-native start
Run Code Online (Sandbox Code Playgroud)

在 Xcode 中打开您的项目并按照其文档导入您的 SDK。希望这可以节省您的研究、实验和调试时间。