发布开源框架到CocoaPods

2017-08-03  本文已影响292人  Yochi
CocoaPods

一直在使用CocoPods,对它既熟悉又陌生,CocoaPods到底是个什么鬼?

一、CocoaPods简介

CocoaPods是一个负责管理iOS项目中第三方开源库的工具。CocoaPods的项目源码在Github上管理。该项目开始于2011年8月12日,在这两年多的时间里,它持续保持活跃更新。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和更新第三方开源库的时间
在我们有了CocoaPods这个工具之后,只需要将用到的第三方开源库放到一个名为Podfile的文件中,然后在命令行执行$ pod install
命令。CocoaPods就会自动将这些第三方开源库的源码下载下来,并且为我的工程设置好相应的系统依赖和编译参数


二、CocoaPods安装

CocoaPods是一个用Ruby写的、负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的、统一管理第三方开源库,为我们节省设置和更新第三方开源库的时间。
因为Mac电脑自带Ruby环境,我们就只需打开终端些人安装命令即可

gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.org/

终端输出:
https://rubygems.org/ removed from sources
gem sources -l

终端输出:
 *** CURRENT SOURCES ***
 https://gems.ruby-china.org/

sudo gem install cocoapods

=========可能出现的状况(坑)=========

问题一:While executing gem ... (Errno::EPERM)  Operation not permitted - /usr/bin/fuzzy_match    错误
解决方案 :
执行sudo gem install -n /usr/local/bin cocoapods  语句。然后提示gems installed即可。
问题二:Error installing pods:active support requires Ruby version >= 2.2.2
解决方案 :
查看ruby版本

$ruby -v
终端会输出你的ruby 版本信息

查看目前的所有ruby版本:

$rvm list known
如果提示command not found 请先安装rvm

$curl -L get.rvm.io | bash -s stable
如果已安装会列出所有的ruby版本:

MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.8]
[ruby-]2.2[.4]
[ruby-]2.3[.0]
[ruby-]2.2-head
ruby-head
......
安装2.2.2:

$rvm install 2.2.2

三、CocoaPods集成iOS开源框架

$ pod search AFNetworking
在终端使用cd +路径切换到项目所在文件下,然后输入:

Vim Podfile

Vim 简单操作
i 进入编辑模式 ESC 退出到 命令模式
:wq 保存退出 :q! 不保存直接退出

就可以在项目目录里看到Podfile文件。也可以使用

pod init
来创建,打开Podfile文件:

open Podfile
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
target 'Yochi' do // 你的项目名称,自行替换

# Uncomment this line if you're using Swift or would like to use dynamic frameworks
# Pods for LNTestDemo
pod 'AFNetworking', '~>3.1.0'
pod 'SDWebImage'
pod 'MBProgressHUD'
pod 'FMDB'
pod 'Masonry'
pod 'MJExtension'
pod 'MJRefresh'
# use_frameworks!
# pod 'ReactiveCocoa'
end

pod install

1.使用pod install来安装新的库,即使你的工程里面已经有了Podfile,并且已经执行过pod install命令了;所以即使你是添加或移除库,都应该使用pod install。
2.使用pod update [PODNAME] 只有在你需要更新库到更新的版本时候用。

无论是执行pod install还是pod update卡在了Analyzing dependencies不动原因在于当执行以上两个命令的时候会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少。加参数的命令如下:

pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
 1)删除工程文件夹下的Podfile
Podfile.lock和Pods文件夹。

2)删除xcworkspace文件。

3)打开xcodeproj文件,删除项目中的Pods文件夹及Pods.xcconfig
引用和libpods.a

4)打开Build Phases选项,删除Check Pods Manifest.lock
和Copy Pods Resources,以及Embeded Pods Frameworks

5.完成,编译运行,无错通过。

四、CocoaPods上发布开源框架

echo "# YochiNavigationBar" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/iyouqiang/YochiNavigationBar.git
git push -u origin master

…or push an existing repository from the command line
git pull
git remote add origin https://github.com/iyouqiang/YochiNavigationBar.git
git push -u origin master
$ git tag -a 1.0.0 -m "v1.0.0"
$ git push origin --tag

终端输出结果:
Counting objects: 1, done.
Writing objects: 100% (1/1), 155 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/iyouqiang/YochiNavigationBar.git
 * [new tag]         1.0.0 -> 1.0.0
pod spec create YochiNavigationBar
该命令将在本目录产生一个名为YochiNavigationBar.podspec文件
$Vim YochiNavigationBar.podspec

库的名称
  s.name         = "YochiNavigationBar"
库原代码版本号
  s.version      = "1.0.0"
对我们库的一个简单的介绍
  s.summary      = “xxoo框架的简短描述"
对我们库的进行完整描述记得头尾不要并行
  s.description  = <<-DESC YochiNavigationBar 
                        是一个快速适配导航栏的工具
                   DESC

替换你的GitHub地址
  s.homepage     = "https://github.com/iyouqiang/YochiNavigationBar.git" 
截屏地址可不写
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" 

MIT许可证(The MIT License)是许多软件授权条款中,被广泛使用的其中一种。与其他常见的软件授权条款(如GPL、LGPL、BSD)相比,MIT是相对宽松的软件授权条款。 license下面会进行介绍
   s.license        = "MIT"
    s.license      = { :type => "MIT", :file => "FILE_LICENSE" }
默认会给你写上
  s.author             = { "iyouqiang" => "iyouqiang@icloud.com" } 
默认会给你写上
    Or just: s.author    = "iyouqiang" 
默认会给你写上 ,多个 ','分开
    s.authors            = { "iyouqiang" => "iyouqiang@icloud.com" } 
这里我写的简书地址
    s.social_media_url   = "http://www.jianshu.com/u/1069cf283571"
支持的平台和版本 iOS8.0
    s.platform     = :ios
    s.platform     = :ios, "8.0"
你的框架地址 版本好不用填写引用上面的版本号,替换GitHub地址即可
  s.source       = { :git => "https://github.com/iyouqiang/YochiNavigationBar.git", :tag => "#{s.version}" }
指定的目录下的文件都会进行编译。如果该目录下还有一些资源文件(如图片等),这些文件并不需要进行编译。可以使用s.resourcs声明。*.{h,m}是一个类似正则表达式的字符串,表示匹配所有以.h和.m为扩展名的文件。
   s.source_files  = ="YochiNavigationBar/**/*.{h,m}"
资源文件单个和多个写在此处
   s.resource  = "icon.png"
   s.resources = "Resources/*.png"
单个依赖库
  s.framework  = "UIKit"
如果你的项目中依赖多个库,可以使用
  s.frameworks = "SomeFramework", "AnotherFramework"
如果依赖第三方库如下写法
  s.dependency "JSONKit", "~> 1.4"

修改好后 :wq 保存退出

用到的参数记得去掉前面的#号注释

vim LICENSE 修改年份和姓名复制下面内容即可
The MIT License (MIT)

Copyright (c) 2017 Yochi(修改年份和姓名,将所有内容复制即可)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

$pod spec lint YochiNavigationBar.podspec

OR

$pod spec lint ECGCustomAlertView.podspec --allow-warnings     (忽略警告)

编辑完podspec文件后,需要验证一下这个文件是否可用,如果有任何WARNING或者ERROR都是不可以的,它就不能被添加到Spec Repo中

终端输出结果:
 -> YochiNavigationBar (1.0.0)

Analyzed 1 podspec.

YochiNavigationBar.podspec passed validation.

无法读LICENSE文件,不用紧张不影响后面的流程
[!] Unable to read the license file `/private/var/folders/cp/qf2l6mqx2x31p0tnptbb059m0000gn/T/CocoaPods/Lint/Pods/YochiNavigationBar/LICENSE` for the spec `YochiNavigationBar (1.0.0)`

[!] Unable to read the license file `/private/var/folders/cp/qf2l6mqx2x31p0tnptbb059m0000gn/T/CocoaPods/Lint/Pods/YochiNavigationBar/LICENSE` for the spec `YochiNavigationBar (1.0.0)`

验证邮箱,用户名, 描述
$ pod trunk register iyouqiang@icloud.com 'Yochi' --description='请叫我Yochi'

终端输出结果:
[!] Please verify the session by clicking the link in the verification email that has been sent to iyouqiang@icloud.com

收到验证邮箱后点击验证,然后查看你的注册信息

$pod trunk me

终端输出结果:
  - Name:     Yochi
  - Email:    iyouqiang@icloud.com
  - Since:    August 3rd, 20:55
  - Pods:     None
  - Sessions:
    - August 3rd, 20:55 - December 9th, 20:58. IP: 113.87.89.176
    Description: 请叫我Yochi

如果你的Pod多人维护可以添加其他观察者
$ pod trunk add-owner 简书 简书@163.com

pod trunk push命令会首先验证你本地的podspec文件(是否有错误),之后会上传spec文件到trunk,最后会将你上传的podspec文件转换为需要的json文件。在工程根目录(包含有.podspec)下执行命令:

$pod trunk push
OR
$pod trunk push YochiNavigationBar.podspec
$pod trunk push YochiNavigationBar.podspec --allow-warnings

如果在trunk push过程中报错了,仔细查看一下错误信息。我当初就是使用了podspec文件中描述的版本所没有的API,之后修改podspec文件中s.platform = :ios, "7.0"就可以了。

上传成功结果:

image.png
查看是否可以搜索到你的开源框架
 $pod search YochiNavigationBar 

查找成功:这个过程真的很慢,耐心

image.png
Pod::Spec.new do |s|
  s.name         = "iOS_Util"
  s.version      = "0.10.0"
  s.summary      = "Some iOS Util"
  s.license      = 'MIT'
  s.author       = { "Yochi" => "iyouqiang@icloud.com" }
  s.platform     = :ios, '8'
  s.ios.deployment_target = '6'

  s.subspec 'Common' do |cos|
    cos.source_files = 'iOS_Util/Common/*.{h,m}'
    cos.public_header_files = 'iOS_Util/Common/*.h'
  end

  s.subspec 'Core' do |cs|
    cs.source_files = 'iOS_Util/Core/*.{h,m}'
    cs.public_header_files = 'iOS_Util/Core/*.h'
    cs.dependency 'libextobjc', '0.2.5'
  end

  s.subspec 'Json' do |js|
    js.source_files = 'iOS_Util/Json/*.{h,m}'
    js.public_header_files = 'iOS_Util/Json/*.h'
    js.dependency 'iOS_Util/Core'
  end

  s.subspec 'Bean' do |bs|
    bs.source_files = 'iOS_Util/Bean/*.{h,m}'
    bs.public_header_files = 'iOS_Util/Bean/*.h'
    bs.dependency 'iOS_Util/Core'
  end

  s.subspec 'DB' do |ds|
    ds.source_files = 'iOS_Util/DB/*.{h,m}'
    ds.public_header_files = 'iOS_Util/DB/*.h'
    ds.dependency 'FMDB/standard' ,'~> 2.1'
    ds.dependency 'iOS_Util/Common'
    ds.dependency 'iOS_Util/Core'
  end

  s.subspec 'WebP' do |ws|
    ws.source_files = 'iOS_Util/WebP/*.{h,m}'
    ws.public_header_files = 'iOS_Util/WebP/*.h'
    ws.dependency 'libwebp' ,'~> 0.3.0-rc7'
    ws.frameworks = 'CoreGraphics'
  end

  s.subspec 'Location' do |ls|
    ls.source_files = 'iOS_Util/Location/*.{h,m}'
    ls.public_header_files = 'iOS_Util/Location/*.h'
    ls.dependency 'iOS_Util/Common'
    ls.dependency 'iOS_Util/DB'
    ls.frameworks = 'CoreLocation' ,'MapKit'
    ls.resource = 'iOS_Util/Location/chinaDivision.sqlite'
  end

  s.subspec 'AMR' do |as|
    as.source_files = 'iOS_Util/AMR/**/*.{h,m,mm}'
    as.public_header_files = 'iOS_Util/AMR/**/*.h'
    as.preserve_paths = "iOS_Util/AMR/**"
    as.library   = 'opencore-amrnb','opencore-amrwb'
    as.xcconfig  = { 'LIBRARY_SEARCH_PATHS' => '"$(PODS_ROOT)/iOS_Util/iOS_Util/AMR/lib"' }
  end

  s.subspec 'Cache' do |cas|
    cas.source_files = 'iOS_Util/Cache/*.{h,m,mm}'
    cas.public_header_files = 'iOS_Util/Cache/*.h'
    cas.dependency 'iOS_Util/Common'
  end

  s.subspec 'Preference' do |ps|
    ps.source_files = 'iOS_Util/Preference/*.{h,m,mm}'
    ps.public_header_files = 'iOS_Util/Preference/*.h'
    ps.dependency 'iOS_Util/Json'
  end

  s.requires_arc = true

end

拓展


参考文章:http://www.cocoachina.com/ios/20160301/15459.html
参考文章:http://blog.csdn.net/blog_t/article/details/59570385

上一篇 下一篇

猜你喜欢

热点阅读