Flutter 開(kāi)發(fā)iOS插件 引用三方框架

具體如何創(chuàng)建插件依賴(lài)插件,可參考閑魚(yú)技術(shù)

現(xiàn)在有一個(gè)調(diào)用阿里云上傳圖片的插件alioss,打開(kāi)文件夾其中有個(gè)目錄是ios,仔細(xì)查看有個(gè)alioss.podspec文件,用CocaPods創(chuàng)建過(guò)自己庫(kù)的同學(xué)應(yīng)該不陌生,文件內(nèi)容如下:

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'alioss'
  s.version          = '0.0.1'
  s.summary          = 'A new Flutter plugin.'
  s.description      = <<-DESC
A new Flutter plugin.
                       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'
  s.dependency 'Flutter'
  s.ios.deployment_target = '8.0'
end

1.引用阿里三方庫(kù)我們?cè)诘箶?shù)第二行添加。

  s.dependency 'AliyunOSSiOS'

這樣我們就引用了阿里的pod庫(kù)了,封裝好相關(guān)的上傳代碼,到flutter主工程下的ios目錄下pod install 完成后,就可以測(cè)試上傳文件的代碼了。

2、引用三方靜態(tài)庫(kù)(.a文件)我們?cè)偬砑右恍小?在另一個(gè)插件里有個(gè)連接打印機(jī)功能,就不再重復(fù).podspec文件了)

s.vendored_libraries = 'Classes/LPAPI/CAR_PRINT.a'

然后把靜態(tài)庫(kù)放在相應(yīng)的文件夾路徑下,同樣使用pod install重新構(gòu)建工程,run下會(huì)報(bào)警告報(bào)錯(cuò),內(nèi)容如下:

Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture armv7
Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture arm64
ld: library not found for -lCAR_PRINT
clang: error: linker command failed with exit code 1 (use -v to see invocation)

這時(shí)候需要?jiǎng)h除 flutter主工程的pods下對(duì)應(yīng)的插件(千萬(wàn)要找到引用.a對(duì)應(yīng)的插件)的TARGETS-->Build Settings->Other Linker Flags列表中的-l"CAR_PRINT" 選項(xiàng),再run一下還是報(bào)警告報(bào)錯(cuò),內(nèi)容如下:

Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture armv7
Warning: no rule to process file '/Users/xxx/development/FlutterPlugin/xxx/ios/Classes/LPAPI/CAR_PRINT.a' of type archive.ar for architecture arm64
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_LPAPI", referenced from:
      objc-class-ref in PrinterViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

說(shuō)明PrinterViewController文件引用了CAR_PRINT.a的頭文件。
這時(shí)候我們需要TARGETS-->Build Phases-->Compile Sources 刪除CAR_PRINT.a文件,再在Compile Sources同級(jí)的另一個(gè)選項(xiàng)Link Binary With Libraries 里添加剛剛刪除的CAR_PRINT.a文件。

最后再run一下,控制臺(tái)顯示內(nèi)容如下:

Launching lib/main.dart on iPhone 8 in debug mode...
Xcode build done.                                           51.9s

最后我們還要根據(jù)引用.a靜態(tài)庫(kù)的提示是否要在插件的TARGETS-->Build Settings->Other Linker Flags列表中加入-ObjC或-all_load等其他flag.

如果上述步驟出錯(cuò)可以使用pod install重置設(shè)置選項(xiàng),還有xcode還是無(wú)法運(yùn)行flutter主工程的,我用的是vscode和vscode flutter插件來(lái)運(yùn)行。

3、引用三方動(dòng)態(tài)庫(kù)Framework的坑還沒(méi)試過(guò),最后的資料里有提到。

最后我們來(lái)一張運(yùn)行圖紀(jì)念一下。

Simulator Screen Shot - iPhone 8 - 2019-04-04 at 13.14.10.png

想詳細(xì)了解xxxx.podspec文件的可以移步官網(wǎng)解釋技術(shù)帖子

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容