cocoaPods类库管理工具的使用,第三方库管理

作者:新闻中心

作为一名iOS开发者,你肯定使用了大量的其它供应商的代码,已各种第三方库的方式。试想一下,这将是多么困难的是,如果你要自己从头开始一切!

下载安装CocoaPods之前,我们需要先搭建Ruby环境,这整个过程都是在终端下进行的。

CocoaPods是用ruby写的,并划分成了若干个Gem包。CocoaPods在解析执行过程中最重要的几个包的路径分别是:CocoaPods/CocoaPods、CocoaPods/Core和CocoaPods/Xcodeproj。

删除之前引入的某个第三方库

在工程中找到Podfile文件,注销掉想要删除的某个开源库

澳门新葡京平台游戏 1

podfilexcode.png

,也可以直接打开Podfile文件,直接删除某个开源库,

澳门新葡京平台游戏 2

podfile.png

再在终端中执行一次pod install命令,成功后即可删除开源库。

澳门新葡京平台游戏 3

delete.png

PS: mac系统升级为High Sierra后发现CocoaPods不能用了,执行pod install命令时,报错: -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad ...

然后我直接照着本教程重新更新ruby版本、重新安装了cocoaPods。


工程


  • 库代码存储在您的项目中,浪费空间。

  • 没有集中的地方看到所用到的所有的库。

  • 很难找到并更新一个库的新版本,特别是如果几个库需要一起​​更新。

  • 在项目中下载和使用库,你可能会对这些代码做出改变(使其难以在以后更新它们)。

我不得不承认,我是这么一个标准的只想用软件,不能好好的装个软件的懒惰者。但是,今天,我终于把CocoaPods装上了!!!

1.2安装CocoaPods

使用

为了更好的说明使用方式: 新建一个工程,名称为CocoaPodsTest,目录结构:

澳门新葡京平台游戏 4

content.png

  • 在工程根目录下新建一个Podfile文件,名字必须是Podfile。

    • 可以创建文件再放在工程根目录下

    • 终端进入工程根目录,执行touch Podfile命令创建:

澳门新葡京平台游戏 5

touchpodfile.png

澳门新葡京平台游戏 6

podfile.png

  • 打开Podfile文件(命令: open -e Podfile): 第一次打开此文件是个空白页面,其内容格式应该配置为如下样式:
platform :ios, '7.0'

target 'CocoaPodsTest' do

pod 'AFNetworking', '~> 2.0'
pod 'Masonry'
pod 'SDWebImage'
pod 'MJRefresh'

end

其中platform为配置平台,ios必须是小写且与之前的":"不能有空格,后面的'7.0'为最低iOS SDK版本号,有些第三方库要求最低版本。

其中AFNetworking后面的2.0为当前要引入的AFNetworking版本,如果不写,默认为该第三方开源库的最新版本。

  • 配置好Podfile文件后保存,在工程根目录下终端执行命令: pod install

澳门新葡京平台游戏 7

install.png

绿色文字,提醒我们以后打开项目的时候应该打开生成的CocoaPodsTest.xcworkspace。

澳门新葡京平台游戏 8

content.png

澳门新葡京平台游戏 9

目录.png

如果终端中报黄色字体:

澳门新葡京平台游戏 10

signerror.png

[!] Smart quotes were detected and ignored in your Podfile. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

可能是你输入的单引号不正确,检查是不是输入了中文的单引号。


sudo gem update --system

在我们做iOS开发中,会使用CocoaPods来添加、管理、第三方库或API。CocoaPods是iOS开发中非常常用的类库管理工具。(当然了,如果不用CocoaPods,你也可以直接把这些下载好的库,直接拖进你的项目里面)

终端输入vim Podfile

CocoaPods是iOS项目的依赖管理工具。使用它不需要我们自己去下载第三方开源库的代码并且放进自己的项目中,它会将第三方开源库的代码下载下来并且设置好相应的系统依赖和编译参数。

这个过程可能会需要一段时间,太好了,你现在是设置为使用CocoaPods!

1、Ruby环境

launchpad->其他->终端->打开终端

澳门新葡京平台游戏 11launchpad.png澳门新葡京平台游戏 12终端.png因为Mac OS本身自带Ruby,因此我们不需要再安装。1.1 在这我们可以先查看一下当前的ruby版本 终端输入命令:

 ruby -v

澳门新葡京平台游戏 131.2 更新ruby版本 终端输入下面三个命令(把Ruby镜像指向taobao,避免被墙)

Rubygems官方网站被Wall了,导致Ruby程序员在线安装软件gem包的时候无法顺利进行,往往需要借助VPN才能成功安装,所以带来了很大的不便。淘宝网提供了Rubygems的国内镜像站: ' '

 gem sources --remove https://rubygems.org/ 移除原网站地址 gem sources -a https://ruby.taobao.org/ 添加淘宝镜像网站 gem sources -l 查看sources

通常情况下,这几个命令结束就完成了,打印结果如下,

澳门新葡京平台游戏 14 更新ruby截图但是,也可能出现我这种情况,因为之前已经替换过镜像,在第一个移除命令时,出现下图澳门新葡京平台游戏 15ruby更新.png 不存在这种情况,这样我们可以直接查看当前的镜像,发现当前已经是 。此时我们可以直接进行下一步:CocoaPods下载了

platform :ios,

补充:

使用第三方库,使用#import引入第三方库头文件时,发现不提醒,command B编译多次仍不提醒。

解决方法:
工程 -> TARGETS -> Build Settings -> Search Paths -> User Header Search Paths:

双击User Header Search Paths后的空白处,点击" "号,输入"$(PODS_ROOT)",修改搜索方式为recursive(递归方式查找)。

澳门新葡京平台游戏 16

header.png

这样在引入头文件时就会自动联想出第三方库的头文件了。

在本教程中,您将学习如何使用名为CocoaPods流行的第三方库管理工具。别急!什么是第三方库管理工具,为什么你需要它?

2、CocoaPods下载

终端输入命令:

 sudo gem install cocoapods 

该命令需要你输入密码,出现后直接输入密码,然后等待就可以,不用着急,等着。。。。知道如图:

澳门新葡京平台游戏 17CocoaPods下载

看到1 gem installed 说明已经下载安装成功了。可能第一次安装的时候,会等待比较久,也会出现其他的信息,这个不会完全一样。但是我们看到1 gem installed就说明已经安装成功。

1.1查看是ruby源

安装
  • 设置ruby源: ruby的软件源rubygems.org因为使用亚马逊的云服务,在国内不能使用,所以要更换ruby源。

打开终端:

澳门新葡京平台游戏 18

terminal.png

  • 查看当前ruby源:
gem sources

结果:

澳门新葡京平台游戏 19

gemsources.png

当前我的mac上的ruby源为:

https://gems.ruby-china.org/

我这是更换后的,如果你的是:

https://rubygems.org/

需要更换为前者。

  • 移除当前ruby源:
gem sources --remove https://rubygems.org/

澳门新葡京平台游戏 20

gemsourcesremove.png

  • 添加新的ruby源:
gem sources -a https://gems.ruby-china.org/

澳门新葡京平台游戏 21

澳门新葡京平台游戏,gemsourcesa.png

再次查看当前ruby源,已经更换:

澳门新葡京平台游戏 22

curren.png

  • 查看当前gem版本:
gem -v

澳门新葡京平台游戏 23

version.png

  • 如果gem版本过低,可以尝试以下方式升级:

使用命令:

gem update --system

报错:

macdeMacBook-Pro:~ mac$ gem update --system
Updating rubygems-update
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

这个暂时没理会,换个命令:

sudo gem update --system

仍然报错:

macdeMacBook-Pro:~ mac$ sudo gem update --system
Password:
Updating rubygems-update
ERROR:  While executing gem ... (OpenSSL::SSL::SSLError)
    hostname "upyun.gems.ruby-china.org" does not match the server certificate

Password:处我输入的为开机密码,不清楚到底是不是。

接着我又执行一次刚才的命令:sudo gem update --system

然后,升级成功了!

Updating rubygems-update
Fetching: rubygems-update-2.6.7.gem (100%)
Successfully installed rubygems-update-2.6.7
Parsing documentation for rubygems-update-2.6.7
Installing ri documentation for rubygems-update-2.6.7
Installing darkfish documentation for rubygems-update-2.6.7
Installing RubyGems 2.6.7
RubyGems 2.6.7 installed
Parsing documentation for rubygems-2.6.7
Installing ri documentation for rubygems-2.6.7

=== 2.6.7 / 2016-09-26

Bug fixes:

* Install native extensions in the correct location when using the
  `--user-install` flag. Pull request #1683 by Noah Kantrowitz.
* When calling `Gem.sources`, load sources from `configuration`
  if present, else use the default sources. Pull request #1699
  by Luis Sagastume.
* Fail gracefully when attempting to redirect without a Location.
  Pull request #1711 by Samuel Giddins.
* Update vendored Molinillo to 0.5.1. Pull request #1714 by
  Samuel Giddins.

=== 2.6.6 / 2016-06-22

Bug fixes:

* Sort installed versions to make sure we install the latest version when
  running `gem update --system`. As a one-time fix, run
  `gem update --system=2.6.6`. Pull request #1601 by David Radcliffe.

=== 2.6.5 / 2016-06-21

Minor enhancements:

* Support for unified Integer in Ruby 2.4. Pull request #1618
  by SHIBATA Hiroshi.
* Update vendored Molinillo to 0.5.0 for performance improvements.
  Pull request #1638 by Samuel Giddins.

Bug fixes:

* Raise an explicit error if Signer#sign is called with no certs. Pull
  request #1605 by Daniel Berger.
* Update `update_bundled_ca_certificates` utility script for directory
  nesting. Pull request #1583 by James Wen.
* Fix broken symlink support in tar writer (  fix broken test). Pull
  request #1578 by Cezary Baginski.
* Remove extension directory before (re-)installing. Pull request #1576
  by Jeremy Hinegardner.
* Regenerate test CA certificates with appropriate extensions. Pull
  request #1611 by rhenium.
* Rubygems does not terminate on failed file lock when not superuser. Pull
  request #1582 by Ellen Marie Dash.
* Fix tar headers with a 101 character name. Pull request #1612 by Paweł
  Tomulik.
* Add Gem.platform_defaults to allow implementations to override defaults.
  Pull request #1644 by Charles Oliver Nutter.
* Run Bundler tests on TravisCI. Pull request #1650 by Samuel Giddins.

=== 2.6.4 / 2016-04-26

Minor enhancements:

* Use Gem::Util::NULL_DEVICE instead of hard coded strings. Pull request #1588
  by Chris Charabaruk.
* Use File.symlink on MS Windows if supported. Pull request #1418
  by Nobuyoshi Nakada.

Bug fixes:

* Redact uri password from error output when gem fetch fails. Pull request
  #1565 by Brian Fletcher.
* Suppress warnings. Pull request #1594 by Nobuyoshi Nakada.
* Escape user-supplied content served on web pages by `gem server` to avoid
  potential XSS vulnerabilities. Samuel Giddins.

=== 2.6.3 / 2016-04-05

Minor enhancements:

* Lazily calculate Gem::LoadError exception messages. Pull request #1550
  by Aaron Patterson.
* New fastly cert. Pull request #1548 by David Radcliffe.
* Organize and cleanup SSL certs. Pull request #1555 by James Wen.
* [RubyGems] Make deprecation message for paths= more helpful. Pull
  request #1562 by Samuel Giddins.
* Show default gems when using "gem list". Pull request #1570 by Luis
  Sagastume.

Bug fixes:

* Stub ordering should be consistent regardless of how cache is populated.
  Pull request #1552 by Aaron Patterson.
* Handle cases when the @@stubs variable contains non-stubs. Pull request
  #1558 by Per Lundberg.
* Fix test on Windows for inconsistent temp path. Pull request #1554 by
  Hiroshi Shirosaki.
* Fix `Gem.find_spec_for_exe` picks oldest gem. Pull request #1566 by
  Shinichi Maeshima.
* [Owner] Fallback to email and userid when owner email is missing. Pull
  request #1569 by Samuel Giddins.
* [Installer] Handle nil existing executable. Pull request #1561 by Samuel
  Giddins.
* Allow two digit version numbers in the tests. Pull request #1575 by unak.

=== 2.6.2 / 2016-03-12

Bug fixes:

* Fix wrong version of gem activation for bin stub. Pull request #1527 by
  Aaron Patterson.
* Speed up gem activation failures. Pull request #1539 by Aaron Patterson.
* Fix platform sorting in the resolver. Pull request #1542 by Samuel E.
  Giddins.
* Ensure we unlock the monitor even if try_activate throws. Pull request
  #1538 by Charles Oliver Nutter.


=== 2.6.1 / 2016-02-28

Bug fixes:

* Ensure `default_path` and `home` are set for paths. Pull request #1513
  by Aaron Patterson.
* Restore but deprecate support for Array values on `Gem.paths=`. Pull
  request #1514 by Aaron Patterson.
* Fix invalid gem file preventing gem install from working. Pull request
  #1499 by Luis Sagastume.

=== 2.6.0 / 2016-02-26

Minor enhancements:

* RubyGems now defaults the `gem push` to the gem's "allowed_push_host"
  metadata setting.  Pull request #1486 by Josh Lane.
* Update bundled Molinillo to 0.4.3. Pull request #1493 by Samuel E. Giddins.
* Add version option to gem open command. Pull request #1483 by Hrvoje
  Šimić.
* Feature/add silent flag. Pull request #1455 by Luis Sagastume.
* Allow specifying gem requirements via env variables. Pull request #1472
  by Samuel E. Giddins.

Bug fixes:

* RubyGems now stores `gem push` credentials under the host you signed-in for.
  Pull request #1485 by Josh Lane.
* Move `coding` location to first line. Pull request #1471 by SHIBATA
  Hiroshi.
* [PathSupport] Handle a regexp path separator. Pull request #1469 by
  Samuel E. Giddins.
* Clean up the PathSupport object. Pull request #1094 by Aaron Patterson.
* Join with File::PATH_SEPARATOR in Gem.use_paths. Pull request #1476 by
  Samuel E. Giddins.
* Handle when the gem home and gem path arent set in the config file. Pull
  request #1478 by Samuel E. Giddins.
* Terminate TimeoutHandler. Pull request #1479 by Nobuyoshi Nakada.
* Remove redundant cache. Pull request #1482 by Eileen M. Uchitelle.
* Freeze `Gem::Version@segments` instance variable. Pull request #1487 by
  Ben Dean.
* Gem cleanup is trying to uninstall gems outside GEM_HOME and reporting
  an error after it tries. Pull request #1353 by Luis Sagastume.
* Avoid duplicated sources. Pull request #1489 by Luis Sagastume.
* Better description for quiet flag. Pull request #1491 by Luis Sagastume.
* Raise error if find_by_name returns with nil. Pull request #1494 by
  Zoltán Hegedüs.
* Find_files only from loaded_gems when using gemdeps. Pull request #1277
  by Michal Papis.


------------------------------------------------------------------------------

RubyGems installed the following executables:
    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/gem

Ruby Interactive (ri) documentation was installed. ri is kind of like man 
pages for ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

RubyGems system software updated

终端过程:

macdeMacBook-Pro:~ mac$ gem sources
*** CURRENT SOURCES ***

https://rubygems.org/
macdeMacBook-Pro:~ mac$ gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
macdeMacBook-Pro:~ mac$ gem sources -a https://gems.ruby-china.org/
https://gems.ruby-china.org/ added to sources
macdeMacBook-Pro:~ mac$ gem sources
*** CURRENT SOURCES ***

https://gems.ruby-china.org/
macdeMacBook-Pro:~ mac$ gem -v
2.0.14.1
macdeMacBook-Pro:~ mac$ ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]
macdeMacBook-Pro:~ mac$ gem update --system
Updating rubygems-update
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
macdeMacBook-Pro:~ mac$ sudo gem update --system
Password:
Updating rubygems-update
ERROR:  While executing gem ... (OpenSSL::SSL::SSLError)
    hostname "upyun.gems.ruby-china.org" does not match the server certificate
macdeMacBook-Pro:~ mac$ sudo gem update --system
Updating rubygems-update
Fetching: rubygems-update-2.6.7.gem (100%)
Successfully installed rubygems-update-2.6.7
Parsing documentation for rubygems-update-2.6.7
Installing ri documentation for rubygems-update-2.6.7
Installing darkfish documentation for rubygems-update-2.6.7
Installing RubyGems 2.6.7
RubyGems 2.6.7 installed
Parsing documentation for rubygems-2.6.7
Installing ri documentation for rubygems-2.6.7

=== 2.6.7 / 2016-09-26

Bug fixes:

* Install native extensions in the correct location when using the
  `--user-install` flag. Pull request #1683 by Noah Kantrowitz.
* When calling `Gem.sources`, load sources from `configuration`
  if present, else use the default sources. Pull request #1699
  by Luis Sagastume.
* Fail gracefully when attempting to redirect without a Location.
  Pull request #1711 by Samuel Giddins.
* Update vendored Molinillo to 0.5.1. Pull request #1714 by
  Samuel Giddins.

=== 2.6.6 / 2016-06-22

Bug fixes:

* Sort installed versions to make sure we install the latest version when
  running `gem update --system`. As a one-time fix, run
  `gem update --system=2.6.6`. Pull request #1601 by David Radcliffe.

=== 2.6.5 / 2016-06-21

Minor enhancements:

* Support for unified Integer in Ruby 2.4. Pull request #1618
  by SHIBATA Hiroshi.
* Update vendored Molinillo to 0.5.0 for performance improvements.
  Pull request #1638 by Samuel Giddins.

Bug fixes:

* Raise an explicit error if Signer#sign is called with no certs. Pull
  request #1605 by Daniel Berger.
* Update `update_bundled_ca_certificates` utility script for directory
  nesting. Pull request #1583 by James Wen.
* Fix broken symlink support in tar writer (  fix broken test). Pull
  request #1578 by Cezary Baginski.
* Remove extension directory before (re-)installing. Pull request #1576
  by Jeremy Hinegardner.
* Regenerate test CA certificates with appropriate extensions. Pull
  request #1611 by rhenium.
* Rubygems does not terminate on failed file lock when not superuser. Pull
  request #1582 by Ellen Marie Dash.
* Fix tar headers with a 101 character name. Pull request #1612 by Paweł
  Tomulik.
* Add Gem.platform_defaults to allow implementations to override defaults.
  Pull request #1644 by Charles Oliver Nutter.
* Run Bundler tests on TravisCI. Pull request #1650 by Samuel Giddins.

=== 2.6.4 / 2016-04-26

Minor enhancements:

* Use Gem::Util::NULL_DEVICE instead of hard coded strings. Pull request #1588
  by Chris Charabaruk.
* Use File.symlink on MS Windows if supported. Pull request #1418
  by Nobuyoshi Nakada.

Bug fixes:

* Redact uri password from error output when gem fetch fails. Pull request
  #1565 by Brian Fletcher.
* Suppress warnings. Pull request #1594 by Nobuyoshi Nakada.
* Escape user-supplied content served on web pages by `gem server` to avoid
  potential XSS vulnerabilities. Samuel Giddins.

=== 2.6.3 / 2016-04-05

Minor enhancements:

* Lazily calculate Gem::LoadError exception messages. Pull request #1550
  by Aaron Patterson.
* New fastly cert. Pull request #1548 by David Radcliffe.
* Organize and cleanup SSL certs. Pull request #1555 by James Wen.
* [RubyGems] Make deprecation message for paths= more helpful. Pull
  request #1562 by Samuel Giddins.
* Show default gems when using "gem list". Pull request #1570 by Luis
  Sagastume.

Bug fixes:

* Stub ordering should be consistent regardless of how cache is populated.
  Pull request #1552 by Aaron Patterson.
* Handle cases when the @@stubs variable contains non-stubs. Pull request
  #1558 by Per Lundberg.
* Fix test on Windows for inconsistent temp path. Pull request #1554 by
  Hiroshi Shirosaki.
* Fix `Gem.find_spec_for_exe` picks oldest gem. Pull request #1566 by
  Shinichi Maeshima.
* [Owner] Fallback to email and userid when owner email is missing. Pull
  request #1569 by Samuel Giddins.
* [Installer] Handle nil existing executable. Pull request #1561 by Samuel
  Giddins.
* Allow two digit version numbers in the tests. Pull request #1575 by unak.

=== 2.6.2 / 2016-03-12

Bug fixes:

* Fix wrong version of gem activation for bin stub. Pull request #1527 by
  Aaron Patterson.
* Speed up gem activation failures. Pull request #1539 by Aaron Patterson.
* Fix platform sorting in the resolver. Pull request #1542 by Samuel E.
  Giddins.
* Ensure we unlock the monitor even if try_activate throws. Pull request
  #1538 by Charles Oliver Nutter.


=== 2.6.1 / 2016-02-28

Bug fixes:

* Ensure `default_path` and `home` are set for paths. Pull request #1513
  by Aaron Patterson.
* Restore but deprecate support for Array values on `Gem.paths=`. Pull
  request #1514 by Aaron Patterson.
* Fix invalid gem file preventing gem install from working. Pull request
  #1499 by Luis Sagastume.

=== 2.6.0 / 2016-02-26

Minor enhancements:

* RubyGems now defaults the `gem push` to the gem's "allowed_push_host"
  metadata setting.  Pull request #1486 by Josh Lane.
* Update bundled Molinillo to 0.4.3. Pull request #1493 by Samuel E. Giddins.
* Add version option to gem open command. Pull request #1483 by Hrvoje
  Šimić.
* Feature/add silent flag. Pull request #1455 by Luis Sagastume.
* Allow specifying gem requirements via env variables. Pull request #1472
  by Samuel E. Giddins.

Bug fixes:

* RubyGems now stores `gem push` credentials under the host you signed-in for.
  Pull request #1485 by Josh Lane.
* Move `coding` location to first line. Pull request #1471 by SHIBATA
  Hiroshi.
* [PathSupport] Handle a regexp path separator. Pull request #1469 by
  Samuel E. Giddins.
* Clean up the PathSupport object. Pull request #1094 by Aaron Patterson.
* Join with File::PATH_SEPARATOR in Gem.use_paths. Pull request #1476 by
  Samuel E. Giddins.
* Handle when the gem home and gem path arent set in the config file. Pull
  request #1478 by Samuel E. Giddins.
* Terminate TimeoutHandler. Pull request #1479 by Nobuyoshi Nakada.
* Remove redundant cache. Pull request #1482 by Eileen M. Uchitelle.
* Freeze `Gem::Version@segments` instance variable. Pull request #1487 by
  Ben Dean.
* Gem cleanup is trying to uninstall gems outside GEM_HOME and reporting
  an error after it tries. Pull request #1353 by Luis Sagastume.
* Avoid duplicated sources. Pull request #1489 by Luis Sagastume.
* Better description for quiet flag. Pull request #1491 by Luis Sagastume.
* Raise error if find_by_name returns with nil. Pull request #1494 by
  Zoltán Hegedüs.
* Find_files only from loaded_gems when using gemdeps. Pull request #1277
  by Michal Papis.


------------------------------------------------------------------------------

RubyGems installed the following executables:
    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/gem

Ruby Interactive (ri) documentation was installed. ri is kind of like man 
pages for ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

RubyGems system software updated
  • 下载命令:
sudo gem install cocoapods

报错:

macdeMacBook-Pro:~ mac$ sudo gem install cocoapods
Password:
Fetching: nanaimo-0.2.3.gem (100%)
Successfully installed nanaimo-0.2.3
Fetching: colored2-3.1.2.gem (100%)
Successfully installed colored2-3.1.2
Fetching: claide-1.0.1.gem (100%)
Successfully installed claide-1.0.1
Fetching: CFPropertyList-2.3.5.gem (100%)
Successfully installed CFPropertyList-2.3.5
Fetching: xcodeproj-1.4.4.gem (100%)
ERROR:  While executing gem ... (Errno::EPERM)
    Operation not permitted - /usr/bin/xcodeproj

找到一种解决方法是:
如果mac系统为OS X EI Capitan可以使用如下命令代替前面的命令执行下载:

2017年09月29日更新: 电脑系统为High Sierra直接使用以下命令

sudo gem install -n /usr/local/bin cocoapods

这里会等待些许时间执行下载:

macdeMacBook-Pro:~ mac$ sudo gem install -n /usr/local/bin cocoapods
Successfully installed xcodeproj-1.4.4
Fetching: ruby-macho-1.1.0.gem (100%)
Successfully installed ruby-macho-1.1.0
Fetching: nap-1.1.0.gem (100%)
Successfully installed nap-1.1.0
Fetching: molinillo-0.5.7.gem (100%)
Successfully installed molinillo-0.5.7
Fetching: gh_inspector-1.0.3.gem (100%)
Successfully installed gh_inspector-1.0.3
Fetching: fourflusher-2.0.1.gem (100%)
Successfully installed fourflusher-2.0.1
Fetching: escape-0.0.4.gem (100%)
Successfully installed escape-0.0.4
Fetching: cocoapods-try-1.1.0.gem (100%)
Successfully installed cocoapods-try-1.1.0
Fetching: netrc-0.7.8.gem (100%)
Successfully installed netrc-0.7.8
Fetching: cocoapods-trunk-1.2.0.gem (100%)
Successfully installed cocoapods-trunk-1.2.0
Fetching: cocoapods-stats-1.0.0.gem (100%)
Successfully installed cocoapods-stats-1.0.0
Fetching: cocoapods-search-1.0.0.gem (100%)
Successfully installed cocoapods-search-1.0.0
Fetching: cocoapods-plugins-1.0.0.gem (100%)
Successfully installed cocoapods-plugins-1.0.0
Fetching: cocoapods-downloader-1.1.3.gem (100%)
Successfully installed cocoapods-downloader-1.1.3
Fetching: cocoapods-deintegrate-1.0.1.gem (100%)
Successfully installed cocoapods-deintegrate-1.0.1
Fetching: fuzzy_match-2.0.4.gem (100%)
Successfully installed fuzzy_match-2.0.4
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.3.gem (100%)
Successfully installed tzinfo-1.2.3
Fetching: minitest-5.10.2.gem (100%)
Successfully installed minitest-5.10.2
Fetching: i18n-0.8.4.gem (100%)
Successfully installed i18n-0.8.4
Fetching: activesupport-4.2.8.gem (100%)
Successfully installed activesupport-4.2.8
Fetching: cocoapods-core-1.2.1.gem (100%)
Successfully installed cocoapods-core-1.2.1
Fetching: cocoapods-1.2.1.gem (100%)
Successfully installed cocoapods-1.2.1
Parsing documentation for xcodeproj-1.4.4
Installing ri documentation for xcodeproj-1.4.4
Parsing documentation for ruby-macho-1.1.0
Installing ri documentation for ruby-macho-1.1.0
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for molinillo-0.5.7
Installing ri documentation for molinillo-0.5.7
Parsing documentation for gh_inspector-1.0.3
Installing ri documentation for gh_inspector-1.0.3
Parsing documentation for fourflusher-2.0.1
Installing ri documentation for fourflusher-2.0.1
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-try-1.1.0
Installing ri documentation for cocoapods-try-1.1.0
Parsing documentation for netrc-0.7.8
Installing ri documentation for netrc-0.7.8
Parsing documentation for cocoapods-trunk-1.2.0
Installing ri documentation for cocoapods-trunk-1.2.0
Parsing documentation for cocoapods-stats-1.0.0
Installing ri documentation for cocoapods-stats-1.0.0
Parsing documentation for cocoapods-search-1.0.0
Installing ri documentation for cocoapods-search-1.0.0
Parsing documentation for cocoapods-plugins-1.0.0
Installing ri documentation for cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-downloader-1.1.3
Installing ri documentation for cocoapods-downloader-1.1.3
Parsing documentation for cocoapods-deintegrate-1.0.1
Installing ri documentation for cocoapods-deintegrate-1.0.1
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for thread_safe-0.3.6
Installing ri documentation for thread_safe-0.3.6
Parsing documentation for tzinfo-1.2.3
Installing ri documentation for tzinfo-1.2.3
Parsing documentation for minitest-5.10.2
Installing ri documentation for minitest-5.10.2
Parsing documentation for i18n-0.8.4
Installing ri documentation for i18n-0.8.4
Parsing documentation for activesupport-4.2.8
unable to convert "x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping
Installing ri documentation for activesupport-4.2.8
Parsing documentation for cocoapods-core-1.2.1
Installing ri documentation for cocoapods-core-1.2.1
Parsing documentation for cocoapods-1.2.1
Installing ri documentation for cocoapods-1.2.1
23 gems installed
  • 安装:
pod setup

这里会在Setting up CocoaPods master repo这个状态等待一段时间:

澳门新葡京平台游戏 24

setup.png

安装报错:

澳门新葡京平台游戏 25

error.png

这可能与网络状态有关系,重试了3次后安装成功。

澳门新葡京平台游戏 26

completed.png


当要求输入密码。终端输出应该是这个样子:

3、CocoaPods添加第三方库

**3.1 **首先,打开Xcode创建一个新工程,起名CocoaPodsManageDemo

澳门新葡京平台游戏 27新工程:CocoaPodsManageDemo

**3.2 **终端中,cd到项目总目录终端输入命令:

 cd /Users/gonganxinxi/Desktop/CocoaPodsManageDemo 

澳门新葡京平台游戏 28cd到项目文件夹.png

在终端输入cd,空格后拖拽你的项目文件夹到终端,会自动出现你的文件夹目录。

**3.3 **建立Podfile终端输入命令:

 vim Podfile

澳门新葡京平台游戏 29建立Podfile.png

enter后,进入Podfile文件,就是在这我们输入我们要导入的第三方库

澳门新葡京平台游戏 30vim Podfile键盘输入 i,进入编辑模式,输入要添加的库命令pod "AFNetworking" (添加AFNetworking库,有很多种表示方式,在该文末尾会进行介绍)输入完毕后键盘 按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq澳门新葡京平台游戏 31键盘输入 i,进入编辑模式澳门新葡京平台游戏 32输入要添加的库命令澳门新葡京平台游戏 33输入完毕后键盘 按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq,然后回车退出,进入终端

附注:比较常用的命令还有**搜索:

 pod search SDW(这个可以写简写,会自动搜索出SDW相应的库及其各个版本)这样你可以直接找到你想要的库,然后粘贴进podfile文件使用。

澳门新葡京平台游戏 34pod search SDW澳门新葡京平台游戏 35pod search SDW

 pod --version 命令可以查看当前cocoapods的版本

**3.4 **Podfile安装及问题解决终端输入命令:

 pod install

澳门新葡京平台游戏 3698290587-61B7-42B4-B751-2C548A345AD2.png

问题1:[!] The dependency AFNetworking is not used in any concrete target.这个库不属于任何target

解决办法:指定target名称target ‘工程名称’ dopod "AFNetworking"end

我们使用vim Podfile命令进入Podfile进行修改

澳门新葡京平台游戏 37指定target名称澳门新葡京平台游戏 38成功添加AFNetworking库

然后我们可以在我们的工程目录中看到CocoaPodsManageDemo.xcworkspace,以后我们就使用该文件打开项目了

澳门新葡京平台游戏 39A90141CE-D952-479D-9158-34C01D2FFC4F.png

pod 'AFNetworking', // 不指定版本号,任何版本都可以,自动会使用最新版本pod 'AFNetworking', '1.0' // 版本号指定为1.0pod 'AFNetworking', '~> 1.0' 版本号可以是1.0,可以是1.1,1.9,但必须小于2

一般有两种情况,1,你的cocoapods需要更新2,依赖库问题。现在解决问题2

澳门新葡京平台游戏 40依赖库问题解决方法Podfile介绍可以参考下面链接

Cocoapods是OS X和iOS下的一个第三方库管理工具。你能使用CocoaPods添加被称作“Pods”的依赖库,并轻松管理它们的版本,而不用考虑当前的时间和开发环境。

参考:

CocoaPods的安装使用和常见问题

更新CocoaPods碰到的问题及知识点

多个TARGETS等请看CocoaPods官方说明

要做到这一点,打开终端,输入以下命令:

澳门新葡京平台游戏 41喵~美美哒

c.利用vim创建Podfile文件,写入以下内容并保存,提示:(终端vim文件按i可编辑,esc退出编辑,:wq可保存退出)

澳门新葡京平台游戏 42c3.png

键盘输入i,进入编辑模式,

如果没有相关的管理工具,你可能只是每个库的代码添加到您的项目。然而,这具有一些缺点:

命令:$ sudo gem uninstall cocoapods

使用第三方库做一些应用非常简单!

2.2使用cocoaPods管理项目

此更新可能需要一段时间,所以要有耐心,并给它一些时间才能完成。如果你好奇,也可以看看终端里面的一些变化。你可以忽略这些变化。接下来,你需要安装CocoaPods。输入端子此命令这样做:

b.查看第三方库是否支持cocoapods,例如查看MJRefresh:

rake's executable "rake" conflicts with /usr/bin/rakeOverwrite the executable? [yN]

1.0cocoaPods核心简介

另外,ruby 的软件源 因为使用的是亚马逊的云服务,所以被墙了,需要更新一下 ruby 的源,使用如下代码将官方的 ruby 源替换成国内淘宝的源:、

把需要添加的三方库通过pod‘库名字’,“~>版本号码"的形式输入完成后,键盘点击esc健,输入:wq结束

要开始,你首先需要安装CocoaPods。 CocoaPods运行在Ruby中,这是它唯一需要的环境。幸运的是,所有最新版本的Mac OS X的(从OS X 10.7开始)已经安装了,因此,所有你需要做的就是更新RubyGems(只是为了确保你有一个最新版本)。

1、安装Ruby和cocoaPods

 sudo gem install cocoapods

3、卸载cocoaPods

你会在本教程中学到第三方库管理工具CocoaPods的使用经验。具体而言,您将创建一个使用多种开源库,获取并从流行的电视和电影信息网站trakt.tv显示信息的应用程序。

成功之后:

gem sources --remove https://rubygems.org/gem sources -a https://ruby.taobao.org/gem sources -l

(4).CocoaPods/ Xcodeproj

一个第三方库管理工具可以帮助你克服这些问题。它将获取源代码库,解决库之间的依赖关系,甚至创造和保持合适的环境,以降低项目的麻烦程度。

d.保证当前目录是在CTHelper目录下,下载导入第三方库命令:

sudo gem update --system

以项目CTHelper为例:

你可以让CocoaPods为你下载和更新第三方库,而不是从GitHub上下载代码,并将其复制到您的项目(从而使将来的更新困难)。

添加ruby源:$gem sources -a

“Show Tracker”APP的介绍

在这里下载本教程的启动代码。这提供了一个基本的用户界面。

打开Main.storyboard,你会看到只有一个视图控制器:

澳门新葡京平台游戏 43C5.png

这是起始的的视图控制器,就取名为 ViewController。

它有一个 UIScrollView 连接了一个 IBOutlet到 showsScrollView 的声明里,一个UIPageControl 连接到 showsPageControl

它也为 showsPageControl设置了一个delegate,并提供了一个简单地方法:pageChanged 它传递showsPageControl 中事件改变的值。

现在,关闭xcode。

安装你第一个第三方库

打开终端,用cd命令符找到 ShowTracker 工程的位置,

cd ~/Path/To/Folder/Containing/ShowTracker

接下去,输入下面的命令:

pod init

他将创造一个默认的 Podfile ,他是你以后更新和添加第三方库的入口。接下去的一行命令用来使用xcode打开这个podfile:

open -a Xcode Podfile

这个默认的 Podfile 看起来像这样:

# Uncomment this line to define a global platform for your project# platform :ios, "6.0"target "ShowTracker" doend

用以下代码替换 # platform :ios, “6.0” :

platform :ios, "7.0"

这告诉CocoaPods你的项目是针对iOS7的 。

许多第三方库 - 包括AFNetworking - 有一个最低的iOS版本要求。如果省略这一行,CocoaPods假定一个默认的目标版本。

终于到了用CocoaPods来添加第一个库了!在 Podfile 中的 target “ShowTracker” do: 后面添加下面代码:

target "ShowTracker" do:

这告诉CocoaPods你要使用AFNetworking 2.2.1版本(本教程写作时的最新版本)作为你的项目库。

这个链接有关于Podfile格式和语法的更多信息。如果你想要做的更复杂的东西(如指定“任何版本高于”一个库的特定版本),你一定要来看看。

保存并关闭 Podfile 。

你现在需要告诉CocoaPods为您的项目为“装”的第三方库。在终端输入以下命令这样做(确保你仍然在含ShowTracker项目Podfile的目录):

pod install

您应该看到输出类似以下内容:

Analyzing dependenciesDownloading dependenciesInstalling AFNetworking Generating Pods projectIntegrating client project

它也可能告诉你,这样的事情:

[!] From now on use `ShowTracker.xcworkspace`.

非常重要!你会在你的项目中看到 ShowTracker.xcworkspace. 这样一个文件,这个Cocoapods为你创建的,记住,以后需要打开这个文件来写代码。关闭Xcode项目和打开 ShowTracker.xcworkspace。

注册Trakt

你可以使用Trakt的API之前,你首先需要注册一个免费账户。别担心 - 它的快速和容易的事情!

注册后,进入setting - > API页面,让您的API KEY。接下来,离开这个页面,但你很快就会需要这个KEY。

注:本来想继续更新的,后来发现trakt.tv这个网址被墙。既然Cocoapods的安装已经实现,那就这样吧。

本周我将用业余时间翻译一篇基于ReactiveCocoa的MVVM开发模式的文章,自己学习下,给大家分享。

cocoaPods核心组件

澳门新葡京平台游戏 44c4.png

使用ruby的gem命令更新pods的命令:

本文翻译自Joshua Greene的博客学习之余,翻译出来贡献给大家,希望大家多多喜欢。我会继续翻译很多文章,供大家学习交流。这里是我之前在CSDN上写的,现在搬家到这里来

等待到终端提示如下内容则证明安装成功:

澳门新葡京平台游戏 45c2.jpg

(3).Podspec

如果是这样,只需 输入y 继续

接下来,我们将通过分析pod安装的过程,一步步揭示CocoaPods背后的技术。

最后,在终端中输入下面的命令来完成CocoaPods的设置:

在下载、安装新的库的同时,会把你安装的每个库的版本都写在Podfile.lock文件里面,这个文件记录你每个安装库的版本号。使用pod install相当于锁定了这些版本,并不会去检查是否该库有新的版本;使用pod update, CocoaPods会帮你更新到这个库的新版本,而不需要考虑Podfile.lock里面的限制,它会更新到这个库尽可能的新版本,只要符合Podfile里面的版本限制.

CocoaPods会使这个项目更加容易。请仔细阅读!

.podspec文件描述了一个库将怎样被添加进工程中。.podspec文件可以标识该第三方库所需要的源码文件、依赖库、编译选项,以及其他第三方库需要的配置。

pod setup

cocoaPods类库管理工具的使用

澳门新葡京平台游戏 46c1.png

Cocoapods意义体现在两个方面。首先,引入第三方库无可避免地要进行各种各样的配置。对于Objective-C的初级开发者来说,项目配置可是一件艰巨的任务。在配置编译阶段和链接器选项的过程中,极有可能引入许多人为的错误。而CocoaPods简化了这一切,它能自动配置编译选项,拯救了开发者。

如果你的 gem 太老,可能也会有问题,可以尝试用如下命令升级 gem:

$pod setup

开始之前:本教程假设您熟悉Xcode中,用命令行(command line)工作,使用AFNetworking和JSON数据格式。如果你对这些东西完全陌生(一个基本的了解,应该没问题),你应该参考本网站的其他教程。根据他的官方网站,CocoaPods是“在Objective-C的项目管理库的依赖关系的最佳途径。”而在很多情况下,这个说的是真的!

Core gem提供了与CocoaPods相关的文件(主要是Podfile和podspecs)的处理。

你可能会在安装过程中收到这样的提示:

(1).CocoaPods / Core

在本教程中,你将建立一个应用程序,使用trakt.tv显示即将到来的电视节目。如果你还没有听说过它,trakt可以帮助你跟踪你观看节目。 Trakt还有一些其他功能,但是在本教程中你只需要用到一个。

有不足的地方欢迎大家批评指正。(1131991303@qq.com  /* 仅限交流编程技术  */)
*

Podfile用于配置项目所需要的第三方库。它能被高度定制,所以你可以尽可能地给它添加你想要的特性。如果您还想对Podfile了解更多的话,请查看Podfile指南(地址http://guides.cocoapods.org/syntax/podfile.html)。

其次,使用CocoaPods可以很方便地查找新的第三方库。当然,这可不是说让你七拼八凑别人代码而开发出一个“移栽”应用。而是让你找到真正好用的库,缩短你的开发周期,提升你的代码质量。

'7.0’

这是面向用户的组件,每当你执行一个pod命令时,这个组件将被激活。它包括了所有实用CocoaPods的功能,并且还能调用其他gem包来执行任务。

ruby的软件源https://rubygems.org因为使用的是亚马逊的云服务,所以被墙了,需要更新一下ruby的源,使用如下代码将官方的ruby源替换成国内淘宝的源.

输入

注:以后再次打开此项目的时候,打开xxx.xcworkspace即可

f.下载完成之后项目的目录结构:

2、cocoaPods的使用

pod‘AFNetworking’,“~>3.0"

$pod install

a.新建Xcode项目CTHelper,使用终端cd到该项目目录里面;

$pod setup

(备注:苹果系统升级OS X EL Capitan后改为$sudo gem install -n /usr/local/bin cocoapods1.3)

$ cd /Users/LYPC/Documents/PodsDemo/CTHelper

pod 'MJRefresh','~> 3.1.0'

$sudo gem install cocoapods

$sudo gem install cocoapods

$pod updateMJRefresh更新MJRefresh到最新版本

$pod search MJRefresh

移除现有Ruby默认源:$gem sources --removehttps://rubygems.org/

这个包负责工程文件直接关系的处理。它能创建以及修改.xcodeproj文件和.xcworkspace文件。它也可以作为一个独立的包使用,当你要编写修改项目文件的脚本时,可以考虑使用CocoaPods/Xcodeproj。

2.1查看cocoapods版本及更新

e.更新指定库到最新版本:

查看当前系统Cocoapods版本命令:$pod --version

如果安装ruby成功,终端出先如下图提示:

$pod update

CocoaPods /CocoaPod

两种命令的区别:

打开终端,输入命令$ gem sources–l来查看源,如果从没有安装过Ruby可以忽略此命令;

(2).Podfile

安装命令:

本文由新葡京8455发布,转载请注明来源

关键词: