Flutter外掛開發《iOS原生模組開發》

.就是我發表於2019-05-26

上一篇簡單的整理了一下Podspec語法整理,主要是為了這一篇Flutter中iOS原生模組開發。

簡介

在開發Flutte中我們難免會遇到原生元件、外掛或者與原生模組通訊,比如地圖、引入第三方sdk如微信、支付寶等SDK,還有攝像頭SDK,我們必須要用到原生, 當然你也可以用pub.dev/flutter中的,但是這並不是最終的解決之道,Flutter剛發展不久,假如剛好沒有或者並不滿足你的需求,因此這就需要自己動手,俗話說自己動手豐衣足食

用法一,直接引用

這種方法比較簡單就像iOS專案中直接引用,前提是pod search可以搜尋到它。


#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'flutter_txmap_plugin'
  s.version          = '0.0.1'
  s.summary          = '一款騰訊地圖Flutter外掛'
  s.description      = <<-DESC
一款騰訊地圖Flutter外掛
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'my_snail@126.com' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  ## 引入,還可以攜帶版本號
  s.dependency 'AFNetworking', '~> 1.0'

  s.ios.deployment_target = '9.0'
end

複製程式碼

用法二,將第三方庫copy到新建的資料夾中

這種方法我們不可以像第一種直接引入,因為pod search根本搜尋不到,比如騰訊地圖,遇到這種方式的話只有下載他們的庫,然後引入,例如:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'flutter_txmap_plugin'
  s.version          = '0.0.1'
  s.summary          = '一款騰訊地圖Flutter外掛'
  s.description      = <<-DESC
一款騰訊地圖Flutter外掛
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'my_snail@126.com' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  # 引入資源,比如我們想要顯示標註的圖片
  s.resources = ['Images/*.png']
  # 騰訊地圖的framework庫,pod search搜尋的應該是很早的版本和最新的api對應不上
  s.vendored_frameworks = 'Frameworks/QMapKit.framework', 'Frameworks/TencentLBS.framework'

  s.ios.deployment_target = '9.0'
end

複製程式碼

第三種,假如不是framework庫而是.a檔案

最近在引入一個攝像頭sdk和配置裝置到Wi-Fi下面,都是.a的庫,這種方式又是如何引入了,也是搞了好久,終於在guides.cocoapods.org/syntax/pods…中找到了 如下:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'erazl_plugin'
  s.version          = '0.0.1'
  s.summary          = 'A new flutter plugin project.'
  s.description      = <<-DESC
A new flutter plugin project.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => 'email@example.com' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.public_header_files = 'Classes/**/*.h', 'Library/*.h'
  # 引入Library資料夾下所有的*.a庫
  s.vendored_libraries = 'Library/*.a'
  s.frameworks = 'MobileCoreServices', 'CFNetwork', 'CoreGraphics'
  # 只是引入*.a庫還是不行,還是要引入相關聯的系統庫,否則無法編譯通過
  # 使用者目標(應用程式)需要連結的系統庫列表,
  s.libraries = 'z.1.2.5', 'c++', 'c', 'iconv.2.4.0', 'sqlite3', 'stdc++.6.0.9', 'xml2', 'bz2.1.0', 'resolv', 'xml2', 'z'
  s.dependency 'Flutter'

  s.ios.deployment_target = '9.0'
end


複製程式碼

上訴就是最近在開發iOS原生模組時遇到的各種問題,在此做個簡單的總結,如果還有其他方式歡迎互相學習

Flutter外掛開發《iOS原生模組開發》

相關文章