Flutter Module混合开发-iOS, flutter 生成frameworks,ios cocoapods集成

565 阅读2分钟

1、现在flutter目录下,创建CocoaPods私有库

 pod lib create MyFlutterFramework
 xingkunkun:FlutterForFW admin$ pod lib create MyFlutterFramework
 Cloning `https://github.com/CocoaPods/pod-template.git` into `MyFlutterFramework `.
Configuring MyFlutter template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.

What platform do you want to use?? [ iOS / macOS ]
 > ios
What language do you want to use?? [ Swift / ObjC ]
 > objc
Would you like to include a demo application with your library? [ Yes / No ]
 > no
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > none
Would you like to do view based testing? [ Yes / No ]
 > no
What is your class prefix?
 >

Running pod install on your new library.

2、flutter 打包

 flutter build ios --debug 
 或者 
flutter build ios --release --no-codesign(选择不需要证书)

3、将CocoaPods私有库集成到Native项目中

在MyFlutterFramework中创建ios_frameworks文件夹,并将 flutter 中build/ios/Release-iphoneos 下的frameworks 复制到 ios_frameworks 文件中 如果有Runner 应用程序,需要过滤Runner

在MyFlutterFramework的podspec文件中,添加以下配置:

  s.static_framework = true
  arr = Array.new
  arr.push('ios_frameworks/*.framework')
  s.ios.vendored_frameworks = arr

注意这里需要加ios_frameworks 目录下所有的frameworks文件添加进去,所以需要改成arr.push('ios_frameworks/**/*.framework')

4、在Ios工程下的podfile文件中添加

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

  # Pods for MyApp
   pod 'MyFlutterFramework', :path => '../MyFlutterFramework'

end

之后在ios的podfile同级目录中执行

pod install

这时在ios中,就可以找到.framework 资源了

如果想要不想要本地资源,可以把MyFlutterFramework推送到远程仓库, 将pod 'MyFlutterFramework'依赖修改为MyFlutterFramework远程连接。

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

  # Pods for MyApp
   pod 'MyFlutterFramework', :git=>'https://gitlab.com/MyFlutterFramework.git', :tag =>"0.1.1"

end

5、发布MyFlutterFramework 时遇到的问题

pod trunk push --allow-warnings

[!] Found podspec `MyFlutterFramework.podspec`
Updating spec repo `trunk`
Validating podspec
 -> MyFlutterFramework (0.1.1)
    - WARN  | source: Git SSH URLs will NOT work for people behind firewalls configured to only allow HTTP, therefore HTTPS is preferred.
    - WARN  | summary: The summary is not meaningful.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | xcodebuild:  note: Build preparation complete
    - NOTE  | [iOS] xcodebuild:  note: Planning
    - NOTE  | [iOS] xcodebuild:  note: Building targets in dependency order
    - NOTE  | [iOS] xcodebuild:  warning: [CP] Vendored binary '/Users/huangrongfeng/Library/Developer/Xcode/DerivedData/App-dwoqtcytrxtpywemwxkuytufucea/Build/Intermediates.noindex/Pods.build/Release-iphonesimulator/MyFlutterFramework.build/DerivedSources/camera_avfoundation.framework.dSYM/Contents/Resources/DWARF/camera_avfoundation' contains architectures (armv7 arm64) none of which match the current build architectures (x86_64).
contains architectures (armv7 arm64) none of which match the current build architectures (x86_64).

这个错误需要 在podspec 中添加

 s.pod_target_xcconfig = { 'VALID_ARCHS' => 'x86_64 armv7 arm64' }

以上时集成中遇到的问题