搭建私有库以及个人遇到的一些问题总结

搭建私有库以及个人遇到的一些问题总结

作为一名资深的 iOS开发,在开发过程中会用到大量的 category,以及一些常用的宏定义和github 一些常用的开源库,如果全都是网往目中拖得话会显得比较无力、麻烦,这本身就是组件化程度比较高的东西,完全没有必要一个一个的往项目中拖,现在有一个非常好用的方法来分享给大家。(当然了网上的博客很多,但是我要打造一个非常简洁便利的方法来让大家去管理自己的私有库)。

  • 1.首先你得有一个 github,或者 coding亦或是 oschina 等代码托管平台的账号(个人推荐 Coding 和 oschina,原因是 Coding,和oschina 是可以免费创建私有项目的,而github是需要收费的)
  • 2.然后呢你需要在 你得代码托管平台上创建两个项目(一个是你得私有源,另一个是你想要引用的私有库)
    那就拿我的我的做个例子吧~
    这是我的私有源
    私有源仓库1
    这是我要引用的私有库
    这是我要引用的私有库
  • 3.创建完仓库之后,紧接着在本地创建两个文件夹,和创建仓库时的名字一样YCPodSpec(下文称地址1),和YCBaseCategory(下文称地址2)。

  • 4.然后呢在在地址2中创建一个你的项目比如说我的(下面的 category 就是你在 cocoapods 中即将要引用的东西)
    创建的项目
    然后在终端输入命令
    cd 地址2中你创建项目的路径(一般直接拖就 OK)
    pod spec create YCBaseCategory
    然后在你的地址2中会出现一个叫 YCBaseCategory.podspec的文件,用 Xcode 打开它编辑
    像我这样对应改一下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Pod::Spec.new do |s|
    s.name = "YCBaseCategory"
    s.authors = "???"
    s.homepage = "git@git.coding.net:???/YCBaseCategory.git"
    s.summary = "YCBaseCategory"
    s.version = "1.0.0"
    s.platform = :ios, "8.0"
    s.source = { :git => "git@git.coding.net:???/YCBaseCategory.git", :tag => s.version }
    s.source_files = "YCBaseCategory/Category/*.{h,m}"
    #s.dependency 'MJRefresh'
    end

然后呢在打开命令行输入以下命令

1
2
3
4
5
git init
git add .
git commit -m 'category'
git remote add origin https://git.coding.net/???/YCBaseCategory.git
git push -u origin master

添加 tag用来区分版本

1
2
git tag 1.0.0
git push --tags

现在呢距离完成已经到了50%了

  • 5.还记得另一个仓库的名字么 YCPodSpec这时候私有源该上场了,首先你把地址1添加到你的 cocopods 的源中去
    pod repo add YCPodSpec https://git.coding.net/???/YCPodSpec.git
    这里呢如果遇到这个问题的就是托管平台没有配公钥和私钥
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     -> YCBaseCategory (1.0.0)
    - WARN | attributes: Missing required attribute `license`.
    - WARN | license: Missing license type.
    - WARN | source: Git SSH URLs will NOT work for people behind firewalls configured to only allow HTTP, therefore HTTPS is preferred.
    - WARN | url: There was a problem validating the URL git@git.coding.net:CoderAn/YCBaseCategory.git.
    - ERROR | [iOS] unknown: Encountered an unknown error ([!] /usr/bin/git clone git@git.coding.net:CoderAn/YCBaseCategory.git /var/folders/4y/z_qs1rx56892cbwxb53kkhyw0000gn/T/d20170717-9015-13p049l --template= --single-branch --depth 1 --branch 1.0.0
    Cloning into '/var/folders/4y/z_qs1rx56892cbwxb53kkhyw0000gn/T/d20170717-9015-13p049l'...
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.
    ) during validation.
    Analyzed 1 podspec.
    [!] The spec did not pass validation, due to 1 error.
    [!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
    `echo "2.3" > .swift-version`.

解决:添加公钥和私钥
** 终端输入

```**
1
2
然后在这个路径中
``` cd ~/.ssh

把生成好的公钥复制到托管平台这里去(以 coding 为例)
配置公钥

检查加没加到 cocoapods源中去
输入命令

~/.cocoapods/repos ```
1
``` ls

我的私有源

如果没有出现问题呢就在地址1中创建一个文件夹名称YCBaseCategory(就是你需要被管理的私有库),然后在里面在创建相关的版本号(就是刚才你项目中的 tag 值1.0.0)类似于这样
Markdown
然后呢在对应版本号中把这个文件复制进来

1
紧接着呢开始验证

pod spec lint YCBaseCategory.podspec –allow-warnings

1
然后推到 代码托管平台

git init
git add .
git commit -m “first commit”
git remote add origin https://git.coding.net/CoderAn/YCPodSpec.git
git push -u origin master

1
2
3
4
5
6
7
8
9

更新你的私有源
然后进入你任意的一个项目在 Podfile 中添加
![在 Podfile 中添加](http://upload-images.jianshu.io/upload_images/1060239-08aecd26d41a806c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#对了还有最重要的一点---你的私有源没有更新到最新版现在你的私有源还是空的
那咱们就更新吧
终端
```
cd 你的任意一个项目

1
pod repo update (这是升级 cocoapods 中所有的源,包括 cocoapods 默认的那个私有源)

你要是升级某一个呢就用这个命令吧

1
pod repo update YCPodSpec(替换成你的源)

如果没有报红的话就是成功了,一般没什么问题如果有的话下面评论,或者自行百度

然后紧接着在你刚才修改Podfile的那个项目

1
pod install

哈哈大功告成1.0版本的私有源成功面世
来看看我的吧
来看看我的吧
哈哈哈现在你还在等什么赶快行动起来吧~~~

##可能有些同学会遇到一些问题:

#####问题1.

Insecure world writable dirin PATH, mode 040777。```
1
2
3
4
5
6
7
8
9
10
11

提示含义:
目录被赋予777的权限,不安全。

如何解决:
将提示权限改为775,问题即可解决。

具体实现:
在终端写入下列命令行,即可消除上面的提示。

```sudo chmod go-w /usr/local/bin

chmod 775link
1
2
3

#####问题2.
```/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- colored2 (LoadError)

提示缺少 colored2, 可使用
sudo gem install colored2
进行安装, 提示缺什么就装什么。