如何快速用上Swift靜態(tài)庫(kù)詳解
前言
Swift 支持靜態(tài)庫(kù)打包已經(jīng)有一段時(shí)間了,CocoaPods 也提供了 static_framework 參數(shù)。然而大部分的第三方依賴都沒(méi)有及時(shí)更新。
本文給出個(gè)相對(duì)方便一些的方案用上靜態(tài)庫(kù),個(gè)人感覺(jué)在依賴不那么復(fù)雜的時(shí)候成本還是比較低的。
效果如下:
示例地址:UseStaticFramework
想辦法為每一個(gè) pod 添加 static_framework 是關(guān)鍵。
直接修改 podspec 不太現(xiàn)實(shí),因?yàn)?CocoaPods 并沒(méi)有提供相關(guān)的接口。但是當(dāng)一個(gè) pod 指定 podspec 地址時(shí),這個(gè) podspec 會(huì)被保存在本地。
如果 pod 沒(méi)有更新,pod install 將直接從本地獲取 pod 信息,這就為我們修改 pod 提供了可能。
target 'UseStaticFramework' do pod 'RxSwift', :git => 'https://github.com/ReactiveX/RxSwift.git' end pre_install do |installer| installer.sandbox.specifications_root.children.each do |podspec| if podspec.extname() == '.json' edit_pod_spec podspec end end end def edit_pod_spec(file) code = File.read(file) json = JSON.parse(code) json['static_framework'] = true File.write(file, JSON.generate(json)) end
在 Podfile 中添加以上代碼,執(zhí)行兩次 bundle exec pod install 即可將依賴 RxSwift 變成靜態(tài)庫(kù)。相比單獨(dú)建一個(gè) Specs 方便很多了,特別是在 RxSwift 有更新時(shí),我們也無(wú)需增加成本,執(zhí)行 bundle exec pod update 即可。
有些依賴稍微麻煩些,比如 RxCocoa 。就目前來(lái)看,Swift 靜態(tài)庫(kù)似乎還不能混編,好在 RxCocoa 支持 SPM,在 SPM 中有一個(gè) RxCocoaRuntime 依賴。
創(chuàng)建一個(gè) RxCocoaRuntime.podspec 使用,再調(diào)整一下 RxCocoa 的 podspec 即可,注意添加 SWIFT_PACKAGE 編譯標(biāo)記:
pod 'RxCocoa', :git => 'https://github.com/ReactiveX/RxSwift.git' pod 'RxCocoaRuntime', :podspec => 'https://raw.githubusercontent.com/DianQK/UseStaticFramework/master/RxCocoaRuntime.podspec' def edit_pod_spec(file) code = File.read(file) json = JSON.parse(code) json['static_framework'] = true if json['name'] == 'RxCocoa' json['xcconfig'] = { :OTHER_SWIFT_FLAGS => '$(inherited) "-D" "SWIFT_PACKAGE"' } json['source_files'] = ['RxCocoa/RxCocoa.swift', 'RxCocoa/Common/**/*.{swift}', 'RxCocoa/Traits/**/*.{swift}', 'RxCocoa/Foundation/**/*.{swift}', 'RxCocoa/Runtime/**/*.{swift}', 'Platform/**/*.swift'] json['preserve_paths'] = ['RxCocoa/RxCocoa.h', 'RxCocoa/*.swift', 'RxCocoa/Common/**/*.{swift,h,m}', 'RxCocoa/Traits/**/*.{swift,h,m}', 'RxCocoa/Foundation/**/*.{swift,h,m}', 'RxCocoa/Runtime/**/*.{swift,h,m}', 'Platform/**/*.swift'] json['dependencies'] = { :RxSwift => '~> 4.1', :RxCocoaRuntime => '~> 4.1' } end File.write(file, JSON.generate(json)) end
執(zhí)行兩次 bundle exec pod install
,完成。
Apollo 這種也能搞,稍微麻煩一些,有些代碼沒(méi)有引入 UIKit,最終導(dǎo)致按照上面的方案編譯不過(guò)去。
pod 'SQLite.swift', :git => 'https://github.com/stephencelis/SQLite.swift.git' pod 'SQLiteObjc', :podspec => 'https://raw.githubusercontent.com/DianQK/UseStaticFramework/master/SQLiteObjc.podspec' pod 'Apollo', :git => 'https://github.com/apollographql/apollo-ios.git' pod 'Apollo/SQLite', :git => 'https://github.com/apollographql/apollo-ios.git' # edit_pod_spec if json['name'] == 'SQLite.swift' json['xcconfig'] = { :OTHER_SWIFT_FLAGS => '$(inherited) "-D" "SWIFT_PACKAGE"' } json['dependencies'] = { :SQLiteObjc => '~> 0.11.4' } json['subspecs'] = [{ :name => 'standard', :source_files => 'Sources/{SQLite,SQLiteObjc}/**/*.{swift}', :exclude_files => 'Sources/**/Cipher.swift', :library => 'sqlite3' }] end post_install do |installer| %w(Pods/Apollo/Sources/ApolloSQLite/*.swift).flat_map { |x| Dir.glob(x) }.each do |file| code = File.read(file) unless code.include? "import UIKit" FileUtils.chmod("+w", file) File.write(file, "import UIKit\n" + code) end end end
給這些沒(méi)添加 import UIKit 代碼補(bǔ)上就行了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Swift中通過(guò)疊加UILabel實(shí)現(xiàn)混合進(jìn)度條的方法
這篇文章主要介紹了Swift中通過(guò)疊加UILabel實(shí)現(xiàn)混合進(jìn)度條的方法的相關(guān)資料,需要的朋友可以參考下2016-08-08在SpringBoot中實(shí)現(xiàn)適配器模式的兩種方式
這篇文章主要介紹了在SpringBoot中實(shí)現(xiàn)適配器模式的兩種方式,通過(guò)實(shí)現(xiàn)類定義類型字段實(shí)現(xiàn)和以動(dòng)態(tài)service名稱的方式實(shí)現(xiàn),并且通過(guò)代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫(kù)步驟方法
本篇文章主要介紹了swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫(kù)步驟方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06解析Swift語(yǔ)言面相對(duì)象編程中的繼承特性
這篇文章主要介紹了解析Swift語(yǔ)言面相對(duì)象編程中的繼承特性,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05