前言:
公司常需要提供源码给客户,但是又有部分源码是不想给的,所以,打包成静态库是一个不错的选择,至于是.a还是.framework,看个人选择,如果头文件比较多的,我还是喜欢.framework,简洁,头文件收藏得深,本文章只是记录一下开发流程以供以后翻看,不足之处,请见谅,斧正。
说明:创建Framework的方法一般有两种,一个是xcode创建,一个是cocoaPods创建,因为创建后,需要将它发布到Pods上给人使用的,公司有部分代码需要隐私,所以我选择了用xcode创建了Framework后build出需要版本,再发布Pods上,但是如果代码不需要保密,可以直接用cocoaPods创建,这样子会省很多事情,利用需要用到cocoaPods管理的第三方框架等
1、创建Framework,添加一个简单的打印类
创建Framework
添加打印类
2、编辑、打包SDK,千万切记要General的Deployment Target选择支持的系统版本,尽量选择到最旧的8.0,这个没有选择的话,后面会影响很大
编译出真机和虚拟机使用的.framework,这里可以选择debug或者Release
合并出一个兼容虚拟机和真机使用的.framework,特别说明一下,framework和a库有点区别,合并的文件是.framework里边的红色框住的这个文件,这个很重要
合并命令:lipo -create 真机编辑文件路径 虚拟机编译文件路径 -output 合并后存放文件的路径
至此,已经Framework就算完成了,正如上面所说,我们需要的是提供包给别人用的,所以接下来是如何将SDK放到Pods上面去
3、将Framework提交到github上
说明:因为Pods默认是基于git的代码管理,所以要将SDK丢到Pods上去,首先需要先放到git上。以下操作都是默认你已经有了github的账号了的
-
首先,在git上创建一个空的新项目,特别注意,下面的初始化尽量、最好勾选上,还有那个License,也选择上,后面提交会用到
然后将该项目克隆到本地,我为了方便查找,直接放到了桌面
git clone https://github.com/p****/C*******FrameworkTest.git
-
将上面弄好的SDK放到该项目中,并提交
cd 到.git仓库的同级目录
git add .
git commit -m "注释说明comment log"
git push
git tag 1.0.1
git push --tags
- 成功后会在终端见到一下内容,github的工作就差不多了,现在到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
- 创建.spec文件,该文件主要是配置一些项目到Pods的参数,极为重要。
pod trunk push ./PHYFrameworkTest.podspec
- 配置.spec文件,部分重要的参数我标了一下注释,但是里边的很多参数我没有使用,因为这个SDK是很简单的,而且没有涉及到png和xib,具体参数使用,可另行查阅
#
# 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"//填写地址
# 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
//平台、系统版本必填,否则会报osx相关的警告,也是提交不了,前面打包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就算是完整创建并提交完了,迫不及待去【pod search PHYFrameworkTest】一下,发现并没有找到,更新一下pod,删除掉pod本地的搜索缓存【~/Library/Caches/CocoaPods】,就会搜索到了。以下是最后的成果