如何在iOS的CapacitorJS插件中添加CocoaPods依赖项?

2021年11月29日20:00:10 发表评论 216 次浏览

了解如何轻松地为 iOS 的 CapacitorJS 插件添加本机依赖项。

与为框架开发用于创建混合应用程序的本机插件一样,文档始终假设我们知道在应用程序本机端使用的工具如何工作。其中之一是我们向 iOS 的 CapacitorJS 插件的 Swift 代码添加本机插件的方式。对于 iOS,该插件基本上是 CocoaPods 的 Pod,Swift 的依赖项管理器。可以在 Pod 的规范文件中添加依赖项。

iOS CapacitorJS插件如何添加CocoaPods依赖项?规范描述了 Pod 库的一个版本。它包括有关应从何处获取源、要使用的文件、要应用的构建设置以及其他常规元数据(例如其名称、版本和描述)的详细信息。在你的 Capacitor Plugin 中,你会在根目录中找到这个文件PluginName.podspec,它的名字是这样的,它看起来像这样:

require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
  s.name = 'CapacitorNativeFilepicker'
  s.version = package['version']
  s.summary = package['description']
  s.license = package['license']
  s.homepage = package['repository']['url']
  s.author = package['author']
  s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
  s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
  s.ios.deployment_target  = '12.0'
  s.dependency 'Capacitor'
  s.swift_version = '5.1'
end

iOS CapacitorJS插件如何添加CocoaPods依赖项:你需要做的就是为spec.dependency插件中需要的每个依赖项添加一个新属性。如你所见,Capacitor 本身是一个必需的 pod,所以如果我想在我的插件中添加原生 Facebook 登录 SDK,我只需添加如下依赖项:

require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
  # Rest of the file
  
  s.dependency 'FacebookShare'
  s.dependency 'FacebookLogin'
  s.dependency 'FacebookCore'

  # If you need to specify a version, you only need to separate it with a comma
  s.dependency 'GoogleAnalytics', '~> 1.0.0'

  # Rest of the file
end

所以当安装插件的开发者使用以下命令同步项目时:

npx cap sync

它将基本上更新 iOS 本机依赖项pod install

✔ Copying web assets from dist to android/app/src/main/assets/public in 736.17ms
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.56ms
✔ copy android in 748.20ms
✔ Updating Android plugins in 1.47ms
[info] Found 4 Capacitor plugins for android:
       @capacitor/app@1.0.2
       @capacitor/clipboard@1.0.2
       @capacitor/filesystem@1.0.2
[warn] @capacitor/core@3.2.0 version doesn't match @capacitor/android@3.1.2 version.
       Consider updating to a matching version, e.g. w/ npm install @capacitor/core@3.1.2
✔ update android in 28.47ms
✔ Copying web assets from dist to ios/App/App/public in 525.16ms
✔ Creating capacitor.config.json in ios/App/App in 223.17μp
✔ copy ios in 528.59ms
✔ Updating iOS plugins in 1.41ms
[info] Found 4 Capacitor plugins for ios:
       @capacitor/app@1.0.2
       @capacitor/clipboard@1.0.2
       @capacitor/filesystem@1.0.2
       capacitor-native-filepicker@0.0.1
✔ Updating iOS native dependencies with pod install in 8.06s
✔ update ios in 8.07s
✔ copy web in 205.46μp
✔ update web in 160.33μp
[info] Sync finished in 9.376s

快乐编码❤️!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: