Framework using three third-party libraries

Contents

Use Cocoapodsto 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 CocoaPodsto 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, CocoaPodsthe AFN source packed into one , add the project to App


Lib into the root directory of the project, the editors MyLib.podspecadd 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 podspecpod lib lint

  • Output after a successful

  • If it fails, follow the prompts to complete all the operations can be

  • note: 一定要先创建远程git仓库,来管理Lib工程


Enter Lib工程/Example/, execute pod installorpod 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工程路径

  • Add-source path 本地路径
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
  • 给此次提交打一个tag,以及将tag推到服务器
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文件是否编写正确,在库工程的根目录下执行

1
pod lib lint

打包生成类库

安装打包插件 cocoapods-packager,这个插件对引用的三方库进行重命名很好的解决了类库命名冲突的问题

1
sudo gem install cocoapods-packager

打包成 .a 或 .framework

  • 打包成 .a
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/查看添加的

1
MyLib

然后执行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 文件

  • PodSpecWhen 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 lintto monitor whether the correct file syntax podspec

  • Execute pod installdownloading 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 tagto a remote git repository

  • push all the source code
  • The submission of a tag set
  • push tag

  • Write again PodSpecfile

  • 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 Repothe
  • 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

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/12361698.html