前言:
公司常需要提供源碼給客戶喳张,但是又有部分源碼是不想給的,所以岂丘,打包成靜態(tài)庫(kù)是一個(gè)不錯(cuò)的選擇陵究,至于是.a還是.framework,看個(gè)人選擇,如果頭文件比較多的元潘,我還是喜歡.framework,簡(jiǎn)潔畔乙,頭文件收藏得深,本文章只是記錄一下開發(fā)流程以供以后翻看翩概,不足之處牲距,請(qǐng)見諒,斧正钥庇。
說(shuō)明:創(chuàng)建Framework的方法一般有兩種牍鞠,一個(gè)是xcode創(chuàng)建,一個(gè)是cocoaPods創(chuàng)建评姨,因?yàn)閯?chuàng)建后难述,需要將它發(fā)布到Pods上給人使用的,公司有部分代碼需要隱私吐句,所以我選擇了用xcode創(chuàng)建了Framework后build出需要版本胁后,再發(fā)布Pods上,但是如果代碼不需要保密嗦枢,可以直接用cocoaPods創(chuàng)建攀芯,這樣子會(huì)省很多事情,利用需要用到cocoaPods管理的第三方框架等
1文虏、創(chuàng)建Framework侣诺,添加一個(gè)簡(jiǎn)單的打印類
創(chuàng)建Framework
添加打印類
2殖演、編輯、打包SDK年鸳,千萬(wàn)切記要General的Deployment Target選擇支持的系統(tǒng)版本趴久,盡量選擇到最舊的8.0,這個(gè)沒有選擇的話搔确,后面會(huì)影響很大
編譯出真機(jī)和虛擬機(jī)使用的.framework彼棍,這里可以選擇debug或者Release
合并出一個(gè)兼容虛擬機(jī)和真機(jī)使用的.framework,特別說(shuō)明一下妥箕,framework和a庫(kù)有點(diǎn)區(qū)別滥酥,合并的文件是.framework里邊的紅色框住的這個(gè)文件,這個(gè)很重要
合并命令:lipo -create 真機(jī)編輯文件路徑 虛擬機(jī)編譯文件路徑 -output 合并后存放文件的路徑
至此畦幢,已經(jīng)Framework就算完成了坎吻,正如上面所說(shuō),我們需要的是提供包給別人用的宇葱,所以接下來(lái)是如何將SDK放到Pods上面去
3瘦真、將Framework提交到github上
說(shuō)明:因?yàn)镻ods默認(rèn)是基于git的代碼管理,所以要將SDK丟到Pods上去黍瞧,首先需要先放到git上诸尽。以下操作都是默認(rèn)你已經(jīng)有了github的賬號(hào)了的
-
首先,在git上創(chuàng)建一個(gè)空的新項(xiàng)目印颤,特別注意您机,下面的初始化盡量、最好勾選上年局,還有那個(gè)License际看,也選擇上,后面提交會(huì)用到
然后將該項(xiàng)目克隆到本地矢否,我為了方便查找仲闽,直接放到了桌面
git clone https://github.com/p****/C*******FrameworkTest.git
-
將上面弄好的SDK放到該項(xiàng)目中,并提交
cd 到.git倉(cāng)庫(kù)的同級(jí)目錄
git add .
git commit -m "注釋說(shuō)明comment log"
git push
git tag 1.0.1
git push --tags
- 成功后會(huì)在終端見到一下內(nèi)容僵朗,github的工作就差不多了赖欣,現(xiàn)在到Pods了
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/phyky/CocoaPodsFrameworkTest.git
* [new tag] 1.0.1 -> 1.0.1
- 注冊(cè)trunk(郵箱和用戶名),通過(guò)會(huì)收到【Please verify the session by clicking the link in the verification email that has been sent to 26******90@qq.com】
pod trunk register 26******90@qq.com phyky --verbose
-
去郵箱驗(yàn)證验庙,將圖中的紅框連接拷到瀏覽器打開顶吮,完成驗(yàn)證
- 查看注冊(cè)信息
pod trunk me
- Name: phyky
- Email: 26******90@qq.com
- Since: September 10th, 02:02
- Pods: None
- Sessions:
- September 10th, 02:02 - January 16th, 2019 02:05. IP:
183.62.48.226
- September 10th, 02:29 - January 16th, 2019 02:29. IP:
183.62.48.226
- 創(chuàng)建.spec文件,該文件主要是配置一些項(xiàng)目到Pods的參數(shù),極為重要粪薛。
pod trunk push ./PHYFrameworkTest.podspec
- 配置.spec文件云矫,部分重要的參數(shù)我標(biāo)了一下注釋,但是里邊的很多參數(shù)我沒有使用汗菜,因?yàn)檫@個(gè)SDK是很簡(jiǎn)單的让禀,而且沒有涉及到png和xib,具體參數(shù)使用陨界,可另行查閱
#
# Be sure to run `pod spec lint PHYFrameworkTest.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 http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
Pod::Spec.new do |s|
# ――― 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.
#
s.name = "PHYFrameworkTest"
s.version = "1.0.1" //切記要和Git提交的版本號(hào)一致
s.summary = "Test for PHYFrameworkTest."
# 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!
s.description = <<-DESC
只是測(cè)試一下巡揍,信不信由你(just for test,no content)
DESC//這里的描述必須比上面的summary長(zhǎng),否則報(bào)警告
s.homepage = "https://github.com/phyky/CocoaPodsFrameworkTest.git"http://填寫地址
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See http://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'.
#
#s.license = "MIT (example)"
//特別重要菌瘪,這就是上面提到的新建Git項(xiàng)目的時(shí)候腮敌,初始化需要選擇的LICENSE,沒有這個(gè)提交會(huì)報(bào)錯(cuò)
s.license = { :type => "MIT", :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.
#
s.author = { "" => "" }
# Or just: s.author = ""
# s.authors = { "" => "" }
# s.social_media_url = "http://twitter.com/"
# ――― 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.
#
# s.platform = :ios
//平臺(tái)俏扩、系統(tǒng)版本必填糜工,否則會(huì)報(bào)osx相關(guān)的警告,也是提交不了录淡,前面打包Framework的時(shí)候提到的重要性
s.platform = :ios, "8.0"
# When using multiple platforms
# s.ios.deployment_target = "8.0"
# s.osx.deployment_target = "10.7"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
s.source = { :git => "https://github.com/phyky/CocoaPodsFrameworkTest.git", :tag => "#{s.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.
#
//特別重要捌木,這里邊的.h或者.m文件不能只是配到.framework,否則找不到,報(bào)錯(cuò)
s.source_files = "lib/PHYFrameworkTest.framework/Headers/*.{h}"
#s.exclude_files = "Classes/Exclude"
# s.public_header_files = "Classes/**/*.h"
//特別重要嫉戚,這里必須配置刨裆,否則上傳成功,找不到.framework,只有頭文件
s.vendored_frameworks = 'lib/PHYFrameworkTest.framework'
# ――― 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.
#
//如果有用到資源的彬檀,路徑配置參考上面的s.source_files
# s.resource = "icon.png"
# s.resources = "Resources/*.png"
# s.preserve_paths = "FilesToSave", "MoreFilesToSave"
# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Link your library with frameworks, or libraries. Libraries do not include
# the lib prefix of their name.
#
# s.framework = "SomeFramework"
# s.frameworks = "UIKit", "Foundation"
# s.library = "iconv"
# s.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.
# s.requires_arc = true
# s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# s.dependency "JSONKit", "~> 1.4"
end
- 校驗(yàn)spec文件是否正確帆啃,這步可能會(huì)拋出很多警告或者錯(cuò)誤,對(duì)照著去修改就行
pod spec lint
- 檢驗(yàn)通過(guò)窍帝,提示以下
-> PHYFrameworkTest (1.0.1)
Analyzed 1 podspec.
PHYFrameworkTest.podspec passed validation.
- 提交到Pods努潘,成功會(huì)有以下的提示
pod trunk push ./PHYFrameworkTest.podspec
Congrats
PHYFrameworkTest (1.0.1) successfully published
September 10th, 03:23
https://cocoapods.org/pods/PHYFrameworkTest
Tell your friends!
進(jìn)行到這里,SDK就算是完整創(chuàng)建并提交完了坤学,迫不及待去【pod search PHYFrameworkTest】一下疯坤,發(fā)現(xiàn)并沒有找到,更新一下pod拥峦,刪除掉pod本地的搜索緩存【~/Library/Caches/CocoaPods】贴膘,就會(huì)搜索到了。以下是最后的成果