iOS 具有 Firebase 依赖项的动态框架

iOS Dyanmic Framework with Firebase Dependencies

我们正在开发一个依赖于某些 firebase 依赖项(如登录、分析等)的框架。框架开发完成后,我们会将其分发给我们的客户。

需要注意的事情是

  1. 代码不应该可见(最好的建议是创建 XCFramework)
  2. 如果可能,创建动态框架而不是静态框架
  3. 可以通过 Swift 包管理器或 cocoa 分发pods

我们的尝试

  1. 我们尝试使用 pods 创建一个动态框架,然后创建一个 XCFramework。但是在导入到客户端应用程序时,找不到 pods 模块
  2. 我们创建了静态库并手动添加了 firebase(直接在项目中)而不是 pods,在这种情况下 XCFramework 没有被导入

我们已尝试创建此处提到的 XCFrame Work(用于动态框架)

可以使用隐藏代码伞框架和通用库,但是对于firebase,这种方法是典型的,并且在互联网上的许多地方也不推荐 有什么other/alternative方法可以满足我们的要求吗?

我们现在有完全相同的设置,而且效果很好。希望对你也有帮助。

照顾的事情:

  • 这是一个 XCFramework 发行版。
  • 它由 CocoaPods 分发(尽管在私有 Podspec 存储库中)
  • 这是一个动态框架。

先决条件:

  • CocoaPods 版本 >= 1.10.1
  • Xcode 版本 >= 11.6(虽然可能更低,不确定)

创建 .xcframework 后,您需要为您的框架创建一个 .podspec,它应该如下所示:

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  s.name         = "MyAwesomeSDK"
  s.version      = "1.0.0"
  s.summary      = "Best framework ever: MyAwesomeSDK"
  s.description  = <<-DESC
                   "Best framework ever: MyAwesomeSDK"
                   DESC
  s.homepage     = "http://github.com"
  s.license      = "MIT"
  s.author       = { "ItIsI" => "me@myself.com" }

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  s.platform = :ios
  s.ios.deployment_target = '11.3'

  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  s.source = { :http => '<MyAwesomeSDK.zip> (We're storing our zipped .xcframework in a hosted page)' }
  s.vendored_frameworks = 'MyAwesomeSDK.xcframework'

  s.swift_version = "5.0"

  # ――― Dependencies ―――――――――――――――――――――――――――---――――――――――――――――――――――――――――――― #
  s.dependency 'SwiftProtobuf',   '1.12.0'
  s.dependency 'lottie-ios',      '3.1.8'
  # Any other dependency you might need.
end

然后,我们通过 Podfile 在另一个项目中使用它,看起来像:

platform :ios, '13.0'

# If you're going to have a private Podspec repo, add the source URL here.
# Don't forget to add the original source if you're going to specify another source.
# source 'https://cdn.cocoapods.org/'

target 'Test' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # If you are publishing the SDK publicly or in a private Podspec repository, this is it:
  pod 'MyAwesomeSDK'

  # If not, you should provide the .podspec to your customers, and:
  pod 'MyAwesomeSDK', :podspec => '<path/to/MyAwesomeSDK.podspec>'

  target 'TestTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'TestUITests' do
    # Pods for testing
  end

end

那么,就是这样!当 运行 pod install 时,您应该看到:

Analyzing dependencies
Downloading dependencies
Installing MyAwesomeSDK (1.0.0)
# These are our own:
# ---
Installing SwiftProtobuf (1.12.0)
Installing lottie-ios (3.1.8)
# ---
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 3 total pods installed.

P.S:我们还必须在我们的 Podfile 中添加一个 post_install 设置,否则它不会正确 link 依赖框架:

if ["SwiftProtobuf", "lottie-ios"].include? target.name
  target.build_configurations.each do |config|
    config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
  end
end