前言:
公司常需要提供源碼給客戶,但是又有部分源碼是不想給的,所以,打包成靜態(tài)庫是一個不錯的選擇,至于是.a還是.framework,看個人選擇,如果頭文件比較多的,我還是喜歡.framework,簡潔,頭文件收藏得深,本文章只是記錄一下開發(fā)流程以供以后翻看,不足之處,請見諒,斧正。
說明:創(chuàng)建Framework的方法一般有兩種,一個是xcode創(chuàng)建,一個是cocoaPods創(chuàng)建,因為創(chuàng)建后,需要將它發(fā)布到Pods上給人使用的,公司有部分代碼需要隱私,所以我選擇了用xcode創(chuàng)建了Framework后build出需要版本,再發(fā)布Pods上,但是如果代碼不需要保密,可以直接用cocoaPods創(chuàng)建,這樣子會省很多事情,利用需要用到cocoaPods管理的第三方框架等
1、創(chuàng)建Framework,添加一個簡單的打印類
創(chuàng)建Framework

添加打印類

2、編輯、打包SDK,千萬切記要General的Deployment Target選擇支持的系統(tǒng)版本,盡量選擇到最舊的8.0,這個沒有選擇的話,后面會影響很大
編譯出真機和虛擬機使用的.framework,這里可以選擇debug或者Release

合并出一個兼容虛擬機和真機使用的.framework,特別說明一下,framework和a庫有點區(qū)別,合并的文件是.framework里邊的紅色框住的這個文件,這個很重要

合并命令:lipo -create 真機編輯文件路徑 虛擬機編譯文件路徑 -output 合并后存放文件的路徑

至此,已經(jīng)Framework就算完成了,正如上面所說,我們需要的是提供包給別人用的,所以接下來是如何將SDK放到Pods上面去
3、將Framework提交到github上
說明:因為Pods默認是基于git的代碼管理,所以要將SDK丟到Pods上去,首先需要先放到git上。以下操作都是默認你已經(jīng)有了github的賬號了的
-
首先,在git上創(chuàng)建一個空的新項目,特別注意,下面的初始化盡量、最好勾選上,還有那個License,也選擇上,后面提交會用到
創(chuàng)建項目 然后將該項目克隆到本地,我為了方便查找,直接放到了桌面
git clone https://github.com/p****/C*******FrameworkTest.git

-
將上面弄好的SDK放到該項目中,并提交
導(dǎo)入SDK
cd 到.git倉庫的同級目錄
git add .
git commit -m "注釋說明comment log"
git push
git tag 1.0.1
git push --tags
- 成功后會在終端見到一下內(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
- 注冊trunk(郵箱和用戶名),通過會收到【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
-
去郵箱驗證,將圖中的紅框連接拷到瀏覽器打開,完成驗證
驗證
- 查看注冊信息
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文件,該文件主要是配置一些項目到Pods的參數(shù),極為重要。
pod trunk push ./PHYFrameworkTest.podspec

- 配置.spec文件,部分重要的參數(shù)我標了一下注釋,但是里邊的很多參數(shù)我沒有使用,因為這個SDK是很簡單的,而且沒有涉及到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提交的版本號一致
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
只是測試一下,信不信由你(just for test,no content)
DESC//這里的描述必須比上面的summary長,否則報警告
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項目的時候,初始化需要選擇的LICENSE,沒有這個提交會報錯
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ǒng)版本必填,否則會報osx相關(guān)的警告,也是提交不了,前面打包Framework的時候提到的重要性
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,否則找不到,報錯
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
- 校驗spec文件是否正確,這步可能會拋出很多警告或者錯誤,對照著去修改就行
pod spec lint
- 檢驗通過,提示以下
-> PHYFrameworkTest (1.0.1)
Analyzed 1 podspec.
PHYFrameworkTest.podspec passed validation.
- 提交到Pods,成功會有以下的提示
pod trunk push ./PHYFrameworkTest.podspec
Congrats
PHYFrameworkTest (1.0.1) successfully published
September 10th, 03:23
https://cocoapods.org/pods/PHYFrameworkTest
Tell your friends!
進行到這里,SDK就算是完整創(chuàng)建并提交完了,迫不及待去【pod search PHYFrameworkTest】一下,發(fā)現(xiàn)并沒有找到,更新一下pod,刪除掉pod本地的搜索緩存【~/Library/Caches/CocoaPods】,就會搜索到了。以下是最后的成果



