Contents
Use Cocoapods
to manage the framework of third-party dependencies.
http://guides.cocoapods.org/making/using-pod-lib-create
Create a 库
Project
1
pod lib create lib project name
Then prompted step of selecting the following
programming language
Whether to add Test Framework
Whether to add UI Testing
What is the prefix project
Cococpods automatically created Lib project after the selection, add pods profile xcworkspace
Then create a git repository to manage this project Lib
Creating a warehouse using github
Then the remote git repository, clone to the local
Then copy all the files in the project created earlier Lib directory to the local repository directory
The last push to a remote git repository
1 2 3
git add. git commit -m "info" git push origin master
So a question, that is, 库工程
and 主App工程
refer to the same third party open source library will cause compile error
Such as: The library project uses AFN, the main App project also used the AFN
1
We need all the source files AFN library project, will add a prefix to distinguish zone.
The above is our own manually modified, too much trouble. You can use CocoaPods
to give us complete the above 添加前缀
manual labor
1
CocoaPods packed library project when, by default all the third-party libraries library project dependencies of source files are specified when the uniform added a prefix before creating the Lib project.
Finally, CocoaPods
the AFN source packed into one 库
, add the project to App
Lib into the root directory of the project, the editors MyLib.podspec
add other third library library project dependencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
Pod::Spec.new do |s| s.name = "MyLib" s.version = "0.1.0" s.summary = "A short description of MyLib." s.description = "specific description information ..." s.homepage = "https://github.com/<GITHUB_USERNAME>/MyLib" # s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2" s.license = 'MIT' s.author = { "xiongzenghui" => "[email protected] " } s.source = { :git => "https://github.com/<GITHUB_USERNAME>/MyLib.git", :tag => s.version.to_s } # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' s.platform = :ios, '7.0' s.requires_arc = true # Hosts all source files Pod / the Classes / s.source_files = 'Pod / the Classes / ** / *' # Store all the resource files Pod / Assets / s.resource_bundles = { 'the MyLib' => [ 'Pod / Assets / *. PNG'] } # Header files are specified search positions # s.public_header_files = 'Pod / Classes / ** / *. H' #-Dependent system Framework # s.frameworks = 'the UIKit', 'the MapKit' # Dependence other custom library # s.libraries = 'xxx.framework', ' xxx.framework', 'xxx.framework' # Third party open source library dependent, if there is more need to fill a plurality s.dependency s.dependency 'AFNetworking', '~> 2.6.3' end
When you write, verify that podspec文件
is correct is to use the command in the directory where podspec pod lib lint
Output after a successful
Enter Lib工程/Example/
, execute pod install
or pod install --no-repo-update
1
Locate the directory that contains the files Podfile
Add the library project's source code files
Add-source path Xcode工程路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
MyLib ├── .travis.yml ├── _Pods.xcproject ├── Example │ ├── MyLib │ ├── MyLib.xcodeproj │ ├── MyLib.xcworkspace │ ├── Podfile │ ├── Podfile.lock │ ├── Pods │ └── Tests ├── LICENSE ├── MyLib.podspec ├── Pod │ ├── Assets 添加所有的资源文件 │ └── Classes 添加所有的源码文件 │ └── RemoveMe.[swift/m] └── README.md
在Xcode工程中的: Pods/Development Pods/MyLib/Pod/Classes
目录下,添加 库源码
1 2 3 4 5 6 7 8
#import <Foundation/Foundation.h> @interface NetManager : NSObject + (void)log; @end
1 2 3 4 5 6 7 8 9 10 11 12 13
#import "NetManager.h" //使用AFN第三方库 #import <AFNetworking/AFNetworking.h> @implementation NetManager + (void)log { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSLog(@"manager = %@n", manager); } @end
只要 新增
类、资源文件、依赖的三方库、podspec文件
修改、都需要重新运行pod install --no-repo-update
来更新配置信息
在App工程
中的代码中使用库工程的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 大专栏 Framework三 使用第三方库 ass="line">19 20 21
#import "XZHViewController.h" //导入库工程的代码 #import <MyLib/NetManager.h> @interface XZHViewController () @end @implementation XZHViewController - (void)viewDidLoad { [super viewDidLoad]; //使用库代码 [NetManager log]; } @end
库代码修改完毕之后提交到远程仓库,一定要打tag,然后将tag也推送到远程仓库
1 2 3 4 5 6 7 8 9 10
git add . 当前提交的版本 git commit -a -m 'v0.1.0' 添加远端仓库(clone下来的工程可以不用执行) git remote add origin git地址 推送到远程仓库 git push origin master
1 2 3 4 5
使用当前版本号打一个tag git tag -a 0.1.0 -m 'v0.1.0' 推送tag到远端仓库 git push --tags
库代码提交到远程git仓库之后,编辑库工程根目录下的podspec文件
设置远程git仓库、以及此次打包需要的哪一个tag
1
s.source = { :git => "https://github.com/xiongzenghuidegithub/MyLib.git", :tag => "0.1.0"}
验证podspec文件
是否编写正确,在库工程的根目录下执行
打包生成类库
安装打包插件 cocoapods-packager
,这个插件对引用的三方库进行重命名
很好的解决了类库命名冲突的问题
1
sudo gem install cocoapods-packager
打包成 .a 或 .framework
1
pod package BZLib.podspec --library --force
打包出 .framework
,不添加 --library
参数即可
1
pod package BZLib.podspec --force
在库工程的根目录下,可以找到新增加的目录,其目录下就是生成的framework文件,如下所示:
注意,生成的framework,默认由CocoaPods完成了多cpu架构
的编译,已经完成支持所有的cpu架构
1
i386, x86_64, armv7, armv7s, arm64
需要特别强调的是,该插件通过对引用的三方库进行重命名很好的解决了类库命名冲突的问题。
OK,到此位置就可以轻松的生成framework了。
如果需要使用 pod install
来安装我们的库
首先需要创建一个用于存放 podspec文件
的远程git仓库
1
https://git.coding.net/xiongzenghui/SpecRepo.git
添加一个自己的所有PodSpect文件的远程git仓库
,然后执行如下命令将自己的SpecRepo添加到系统CocoaPods的Spec库
1
pod repo add XiongSpecRepo https://git.coding.net/xiongzenghui/SpecRepo.git
进入 ~/.cocoapods/repos
目录下,查看我们添加的spec仓库
1 2 3
xiongzenghuideMacBook-Pro:MyLib xiongzenghui$ cd ~/.cocoapods/repos xiongzenghuideMacBook-Pro:repos xiongzenghui$ ls XiongSpecRepo master
如果其他人员也需要操作这个spec仓库,需要给予权限
.
将库工程下的PodSpect
文件提交到本地SpecRepo
,如下命令同时将这个podspec文件也Push到远程SpecRepo
1
pod repo push XiongSpecRepo MyLib.podspec
进入到 ~/.cocoapods/repos/XiongSpecRepo/
查看添加的
然后执行pod search 库名
会从本地系统CocoaPods的所有的SpecRepo搜索这个库,最终查询到我们自己的SpecRepo XiongSpecRepo
1 2 3 4 5 6 7 8
xiongzenghuideMacBook-Pro:XiongSpecRepo xiongzenghui$ pod search MyLib -> MyLib (0.1.1) 测试pod生成framework的YohunlUtilsPod. pod 'MyLib', '~> 0.1.1' - Homepage: https://github.com/xiongzenghuidegithub/MyLib - Source: https://github.com/xiongzenghuidegithub/MyLib.git - Versions: 0.1.1 [XiongSpecRepo repo]
注意: 团队内其他项目组需要使用,那么必须手动将之前的XiongSpecRepo
添加到本地CocoaPods的Spec系统库中.
小结主要步骤:
建立 两个
git仓库
远程git仓库1,存放 库工程源码
远程git仓库2,存放 所有的 PodSpec 文件
PodSpec
When used to generate a library file (.a or .framework), reads the configuration file
Specify which library from the git remote, pull library source code
Which tag source acquisition
And source location
Rely on the system framework
Other static library dependencies
Third-party open source libraries dependent
pod lib create 库工程名字
Create a library project
Then write the spec file 库工程名字.podspec
After making all the changes above
Execute commands pod lib lint
to monitor whether the correct file syntax podspec
Execute pod install
downloading dependencies libraries, as well as update all system configuration (dependent on a variety of library files ..)
Note: After each library project are changed, we need to be performed
Then write all source library project
Submit 库工程源码
to a remote git repository, as well as submit tag
to a remote git repository
push all the source code
The submission of a tag set
push tag
Write again PodSpec
file
Modify the corresponding source code library git address
Modify the library source code corresponding tag
Then 库工程的 PodSpec 文件
submitted to the Spec CocoaPods library
The machine can be directly pod install 库名
mounted library
Before other machines must first be added PodSpec git 库
to your local library CocoaPods of Spec, and then perform the above installation command
You can actually see CocoaPods
First github or other git repository, all of the podspec文件
copy to 本地的Spec Repo
the
Then the query 本地podspec文件
to find the source code to download the library git地址、以及tag、依赖关系
, etc.
Learning Source: http://www.cnblogs.com/brycezhang/p/4117180.html