此文僅做備忘,不做任何學(xué)習(xí)交流,大家可隨意參考
framework中三方庫依賴以及framework發(fā)布統(tǒng)一使用pod方式
一、制作framework
1、創(chuàng)建APP工程,用來開發(fā)framework和測試用的
以UIImageView的擴(kuò)展framework制作為例,并在framework中依賴三方庫SDWebImage
2、創(chuàng)建framework的Target,我命名為MyUIImageViewSDK
3、使用pod管理依賴的三方庫(如SDWebImage)
使用終端cd到項目根目錄,執(zhí)行命令
pod init,打開生成的Podfile文件,在MyUIImageViewSDK下添加pod 'SDWebImage'
終端執(zhí)行
pod install,然后打開MyUIImageViewLib.xcworkspace文件
4、配置framework
設(shè)置支持版本
取消只編譯當(dāng)前架構(gòu)
移除重復(fù)的架構(gòu)
真機(jī)和模擬器編譯后,framework中arm64架構(gòu)重復(fù),會導(dǎo)致合并失敗,所以移除模擬器中的arm64架構(gòu)
設(shè)置編譯選項優(yōu)化,給framework包瘦身
設(shè)置類型為靜態(tài)庫
設(shè)置release
5、開發(fā)代碼
如果讓類和方法讓外界可調(diào)用,需要用權(quán)限修飾,我用的是public
swift 不像OC可以暴露接口,在swift中 要想給別的工程調(diào)用接口,記得在類,方法或?qū)傩郧凹觩ublic。
swift權(quán)限控制符:
open 權(quán)限最大,可以被外界模塊訪問,繼承重寫
public 可以被外界工程訪問
internal 默認(rèn)文件創(chuàng)建時的權(quán)限,可以在本工程的訪問
private 只可以在創(chuàng)建的文件內(nèi)訪問
6、添加生成最終需要發(fā)布的framework的腳本
說明:關(guān)于framework編譯和合并的腳本,我還是比較喜歡以下的方式,簡單粗暴。
像新建一個shell腳本的target我也試過,framework中沒有依賴三方還行,像我這種依賴三方庫的,shell運行不通過,三方庫會報錯
carthage編譯framework我也試過,雖然可以解決三方庫報錯的問題,但是我覺得麻煩
# 真機(jī)和模擬器framework合并腳本
# 選中framework,分別在真機(jī)和模擬器編譯成功即可
if [ "${ACTION}" = "build" ]
then
# 定義framework名稱(替換為自己定義的名字即可)
SDK_NAME=MyUIImageViewSDK
# 輸出路徑
INSTALL_DIR=${SRCROOT}/Products/${SDK_NAME}.framework
# 真機(jī)路徑
DEVICE_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphoneos/${SDK_NAME}.framework
# 模擬器路徑
SIMULATOR_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphonesimulator/${SDK_NAME}.framework
# 如果輸出路徑已存在,則刪除
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
# 創(chuàng)建輸出路徑
mkdir -p "${INSTALL_DIR}"
# 如果真機(jī)framework和模擬器framework都存在
if [ -d "${DEVICE_DIR}" ] && [ -d "${SIMULATOR_DIR}" ]
then
# 拷貝真機(jī)framework到輸出路徑
cp -r "${DEVICE_DIR}/" "${INSTALL_DIR}/"
# 拷貝模擬器framework中的modules到輸出路徑
cp -r "${SIMULATOR_DIR}/Modules/${SDK_NAME}.swiftmodule/" "${INSTALL_DIR}/Modules/${SDK_NAME}.swiftmodule"
# 合并真機(jī)framework和模擬器framework
lipo -create "${DEVICE_DIR}/${SDK_NAME}" "${SIMULATOR_DIR}/${SDK_NAME}" -output "${INSTALL_DIR}/${SDK_NAME}"
fi
fi
7、生成framework
選中MyUIImageViewSDK,分別在真機(jī)和模擬器編譯一次即可,然后在項目的根目錄中,Products文件夾里會有最終的framework
二、發(fā)布framework
cocoapods發(fā)布私有庫的大致流程為:
1、GitHub上創(chuàng)建一個私有索引庫,這個索引庫存放的都是私有庫的路徑
2、GitHub上創(chuàng)建一個私有庫,存放的是整個私有庫的所有源碼
3、把私有庫的.podspec文件push到私有索引庫中
4、把私有索引庫更新到本地repo中
創(chuàng)建私有索引庫,并添加到本地repo中
終端執(zhí)行pod repo add MySpec git@github.com:WitXiong/MySpec.git
然后執(zhí)行pod repo即可查看有沒有添加成功
創(chuàng)建私有組件庫
本地組件源碼根目錄下,創(chuàng)建.podspec文件
終端執(zhí)行pod spec create MyUIImageViewSDK
生成文件后拖入Xcode工程中,取消copy勾選
podspec文件修改完后,把本地私有庫代碼和遠(yuǎn)端私有庫關(guān)聯(lián),并把代碼都push上去
這里需要注意的是,每次push后都需要打tag,且tag和.podspec文件中的版本號要一致
#
# Be sure to run `pod spec lint MyUIImageViewSDK.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
Pod::Spec.new do |spec|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# These will help people to find your library, and whilst it
# can feel like a chore to fill in it's definitely to your advantage. The
# summary should be tweet-length, and the description more in depth.
#
spec.name = "MyUIImageViewSDK"
spec.version = "0.0.1"
spec.summary = "A short description of MyUIImageViewSDK."
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
spec.description = <<-DESC
這是描述區(qū),這里的文字一定要比 spec.summary 中的內(nèi)容長,
否則spec遠(yuǎn)端驗證可能會不通過
DESC
# 這里是主頁地址,可以寫github私有庫的主頁地址
spec.homepage = "http://EXAMPLE/MyUIImageViewSDK"
# spec.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See https://choosealicense.com for more info.
# CocoaPods will detect a license file if there is a named LICENSE*
# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
#
spec.license = { :type => "MIT", :file => "FILE_LICENSE" }
# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the authors of the library, with email addresses. Email addresses
# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
# accepts just a name if you'd rather not provide an email address.
#
# Specify a social_media_url where others can refer to, for example a twitter
# profile URL.
#
spec.author = { "wit" => "email@qq.com" }
# Or just: spec.author = "kaigao"
# spec.authors = { "kaigao" => "email@qq.com" }
# spec.social_media_url = "https://twitter.com/kaigao"
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If this Pod runs only on iOS or OS X, then specify the platform and
# the deployment target. You can optionally include the target after the platform.
#
spec.platform = :iOS
spec.platform = :ios, "10.0"
spec.swift_version = "5.0"
# When using multiple platforms
# spec.ios.deployment_target = "5.0"
# spec.osx.deployment_target = "10.7"
# spec.watchos.deployment_target = "2.0"
# spec.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
# 這是私有庫的路徑,填寫私有庫的git clone 的鏈接就行
spec.source = { :git => "git@github.com:WitXiong/MyUIImageViewSDK.git", :tag => "#{spec.version}" }
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# CocoaPods is smart about how it includes source code. For source files
# giving a folder will include any swift, h, m, mm, c & cpp files.
# For header files it will include any header in the folder.
# Not including the public_header_files will make all headers public.
#
# 如果是想暴露源碼,打開這行
# spec.source_files = "Classes", "Classes/**/*.{h,m}"
# spec.exclude_files = "Classes/Exclude"
# 此處暴露的是framework
spec.vendored_frameworks = "Products/MyUIImageViewSDK.framework"
# spec.public_header_files = "Classes/**/*.h"
# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# A list of resources included with the Pod. These are copied into the
# target bundle with a build phase script. Anything else will be cleaned.
# You can preserve files from being cleaned, please don't preserve
# non-essential files like tests, examples and documentation.
#
# spec.resource = "icon.png"
# spec.resources = "Resources/*.png"
# spec.preserve_paths = "FilesToSave", "MoreFilesToSave"
# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Link your library with frameworks, or libraries. Libraries do not include
# the lib prefix of their name.
#
# spec.framework = "SomeFramework"
# spec.frameworks = "SomeFramework", "AnotherFramework"
# spec.library = "iconv"
# spec.libraries = "iconv", "xml2"
# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If your library depends on compiler flags you can set them in the xcconfig hash
# where they will only apply to your library. If you depend on other Podspecs
# you can include multiple dependencies to ensure it works.
# spec.requires_arc = true
# spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# 這是聲明framework依賴的三方庫
spec.dependency "SDWebImage", "~> 5.0"
end
把.podspec文件push到遠(yuǎn)程Spec私有索引庫中
終端執(zhí)行pod repo push MySpec MyUIImageViewSDK.podspec --allow-warnings --skip-import-validation
使用(和正常的pod庫使用方式一樣)
唯一的區(qū)別是,因為是私有庫,需要在podfile文件中額外添加源地址
一個是私有的索引庫地址source 'git@github.com:WitXiong/MySpec.git'
一個是cocoapods的cdn地址source 'https://cdn.cocoapods.org/'
結(jié)束























