当前位置: 代码迷 >> 综合 >> IOS开发(12)IOS Cocoapods 工具安装与使用
  详细解决方案

IOS开发(12)IOS Cocoapods 工具安装与使用

热度:84   发布时间:2024-01-05 12:06:48.0

Cocoapod是ios开发用于管理第三方框架的工具,类似Java中的Maven,至少我是这样理解的。第一次使用,后面还不知道有多少坑,所以记录下学习日志,为后续学习打好基础。

硬件环境

电脑: macOS High Sierra 10.13.3
xcode: Version 9.2

具体安装过程

具体安装过程分为这么几步d,在网上找的帖子教程,大概也是经过这么几个步骤。
1.修改下载源镜像地址
2.升级Ruby
3.安装pod
4.配置pod
5.使用pod

1.修改下载源镜像地址

witsystemdeMacBook-Pro:~ witsystem$ gem sources --add https://gems.ruby-china.org/ --remove https://ruby.taobao.org/
source https://gems.ruby-china.org/ already present in the cache
https://ruby.taobao.org/ removed from sources
witsystemdeMacBook-Pro:~ witsystem$ gem sources -l
*** CURRENT SOURCES ***https://gems.ruby-china.org/

上面命令将移除一个添加另一个。

2.升级Ruby

witsystemdeMacBook-Pro:~ witsystem$ sudo gem update --system
Password:
Updating rubygems-update
Fetching: rubygems-update-2.6.14.gem (100%)
Successfully installed rubygems-update-2.6.14
Parsing documentation for rubygems-update-2.6.14
Installing ri documentation for rubygems-update-2.6.14
Installing darkfish documentation for rubygems-update-2.6.14
Done installing documentation for rubygems-update after 32 seconds
Parsing documentation for rubygems-update-2.6.14
Done installing documentation for rubygems-update after 0 seconds
Installing RubyGems 2.6.14
RubyGems 2.6.14 installed
Parsing documentation for rubygems-2.6.14
Installing ri documentation for rubygems-2.6.14=== 2.6.14 / 2017-10-09Security fixes:* Whitelist classes and symbols that are in loaded YAML.
 See CVE-2017-0903 for full details.
 Fix by Aaron Patterson.=== 2.6.13 / 2017-08-27Security fixes:* Fix a DNS request hijacking vulnerability.
 Fix by Samuel Giddins.
* Fix an ANSI escape sequence vulnerability.
 Fix by Evan Phoenix.
* Fix a DOS vulernerability in the `query` command.
 Fix by Samuel Giddins.
* Fix a vulnerability in the gem installer that allowed
 a malicious gem to overwrite arbitrary files.
 Fix by Samuel Giddins.=== 2.6.12 / 2017-04-30Bug fixes:* Fix test_self_find_files_with_gemfile to sort expected files. Pullrequest #1880 by Kazuaki Matsuo. * Fix issue for MinGW / MSYS2 builds and testing. Pull request #1879 byMSP-Greg. * Fix gem open to open highest version number rather than lowest. Pullrequest #1877 by Tim Pope. * Add a test for requiring a default spec as installed by the rubyinstaller. Pull request #1899 by Samuel Giddins. * Fix broken --exact parameter to gem command. Pull request #1873 by JasonFrey. * [Installer] Generate backwards-compatible binstubs. Pull request #1904by Samuel Giddins. * Fix pre-existing source recognition on add action. Pull request #1883 byJonathan Claudius. * Prevent negative IDs in output of #inspect. Pull request #1908 by VítOndruch. * Allow Gem.finish_resolve to respect already-activated specs. Pull
 request #1910 by Samuel Giddins.=== 2.6.11 / 2017-03-16Bug fixes:* Fixed broken tests on ruby-head. Pull request #1841 by
 SHIBATA Hiroshi.
* Update vendored Molinillo to 0.5.7. Pull request #1859 by Samuel
 Giddins.
* Avoid activating Ruby 2.5 default gems when possible. Pull request #1843
 by Samuel Giddins.
* Use improved resolver sorting algorithm. Pull request #1856 by
 Samuel Giddins.=== 2.6.10 / 2017-01-23Bug fixes:* Fix `require` calling the wrong `gem` method when it is overridden.
 Pull request #1822 by Samuel Giddins.=== 2.6.9 / 2017-01-20Bug fixes:* Allow initializing versions with empty strings. Pull request #1767 by
 Luis Sagastume.
* Fix TypeError on 2.4. Pull request #1788 by Nobuyoshi Nakada.
* Don't output mkmf.log message if compilation didn't fail. Pull request
 #1808 by Jeremy Evans.
* Fixed broken links and overzealous URL encoding in gem server. Pull
 request #1809 by Nicole Orchard.
* Update vendored Molinillo to 0.5.5. Pull request #1812 by Samuel
 Giddins.
* RakeBuilder: avoid frozen string issue. Pull request #1819 by Olle
 Jonsson.=== 2.6.8 / 2016-10-29Bug fixes:* Improve SSL verification failure message. Pull request #1751
 by Eric Hodel.
* Ensure `to_spec` falls back on prerelease specs. Pull request
 #1755 by André Arko.
* Update vendored Molinillo to 0.5.3. Pull request #1763 by
 Samuel Giddins.=== 2.6.7 / 2016-09-26Bug 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-22Bug 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-21Minor 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-26Minor enhancements:* Use Gem::Util::NULL_DEVICE instead of hard coded strings. Pull request #1588by Chris Charabaruk. * Use File.symlink on MS Windows if supported. Pull request #1418by 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-05Minor 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-12Bug 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-28Bug 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-26Minor 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 reportingan 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 byZoltán Hegedüs. * Find_files only from loaded_gems when using gemdeps. Pull request #1277by Michal Papis.=== 2.5.2 / 2016-01-31Bug fixes:* Fix memoization of Gem::Version#prerelease? Pull request #1125 by Matijs van
 Zuijlen.
* Handle trailing colons in GEM_PATH, by Damien Robert. * Improve the Gemfile `gemspec` method, fixing #1204 and #1033. Pull request#1276 by Michael Papis. * Warn only once when a gemspec license is invalid. Pull request #1414 by SamuelE. Giddins. * Check for exact constants before using them, fixing Ruby bug #11940. Pullrequest #1438 by Nobuyoshi Nakada. * Fix building C extensions on Ruby 1.9.x on Windows. Pull request #1453 by MarieMarkwell. * Handle symlinks containing ".." correctly. Pull request #1457 by Samuel E.Giddins.Minor enhancements:* Add `--no-rc` flag, which skips loading `.gemrc`. Pull request #1329 by Luis
 Sagastume.
* Allow basic auth to be excluded from `allowed_push_host`. By Josh Lane.
* Add `gem list --exact`, which finds gems by string match instead of regex. Pull
 request #1344 by Luis Sagastume.
* Suggest alternatives when gem license is unknown. Pull request #1443 by Samuel
 E. Giddins.
* Print a useful error if a binstub expects a newer version of a gem than is
 installed. Pull request #1407 by Samuel E. Giddins.
* Allow the (supported) s3:// scheme to be used with `--source`. Pull request
 #1416 by Dave Adams.
* Add `--[no-]post-install-message` to `install` and `update`. Pull request #1162
 by Josef ?imánek.
* Add `--host` option to `yank`, providing symmetry with `pull`. Pull request
 #1361 by Mike Virata-Stone.
* Update bundled Molinillo to 0.4.1. Pull request #1452 by Samuel E. Giddins.
* Allow calling `build` without '.gemspec'. Pull request #1454 by Stephen
 Blackstone.
* Add support for `source` option on gems in Gemfile. Pull request #1355 by
 Michael Papis.
* Function correctly when string literals are frozen on Ruby 2.3. Pull request
 #1408 by Samuel E. Giddins.------------------------------------------------------------------------------RubyGems installed the following executables:/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/gemRuby Interactive (ri) documentation was installed. ri is kind of like man pages for ruby libraries. You may access it like this:ri Classnameri Classname.class_methodri 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 witsystemdeMacBook-Pro:~ witsystem$ rvm -v -bash: rvm: command not found witsystemdeMacBook-Pro:~ witsystem$ gem RubyGems is a sophisticated package manager for Ruby. This is a basic help message containing pointers to more information.Usage:gem -h/--helpgem -v/--versiongem command [arguments...] [options...]Examples:gem install rakegem list --localgem build package.gemspecgem help installFurther help:gem help commands list all 'gem' commandsgem help examples show some examples of usagegem help gem_dependencies gem dependencies file guidegem help platforms gem platforms guidegem help <COMMAND> show help on COMMAND(e.g. 'gem help install')gem server present a web page athttp://localhost:8808/with info about installed gemsFurther information:http://guides.rubygems.org witsystemdeMacBook-Pro:~ witsystem$ gem -v 2.6.14 

这样就完成Ruby的升级,可以查询版本

3.安装pod

witsystemdeMacBook-Pro:~ witsystem$ sudo gem install -n /usr/local/bin cocoapods
Successfully installed cocoapods-1.4.0
Parsing documentation for cocoapods-1.4.0
Done installing documentation for cocoapods after 2 seconds
1 gem installed
witsystemdeMacBook-Pro:~ witsystem$ pod --version
1.4.0

当出现上面的提示表示安装成功,我安装的是1.4.0版本的

4.配置pod

witsystemdeMacBook-Pro:~ witsystem$ pod setup
Setting up CocoaPods master repo$ /usr/bin/git -C /Users/witsystem/.cocoapods/repos/master fetch origin--progressremote: Counting objects: 72, done.        remote: Compressing objects: 100% (53/53), done.        remote: Total 72 (delta 47), reused 25 (delta 15), pack-reused 0        From https://github.com/CocoaPods/Specsc6592065b49..7f1f6e94652  master     -> origin/master$ /usr/bin/git -C /Users/witsystem/.cocoapods/repos/master rev-parse--abbrev-ref HEADmaster$ /usr/bin/git -C /Users/witsystem/.cocoapods/repos/master reset --hardorigin/masterHEAD is now at 7f1f6e94652 [Add] KZLogger 0.1.7 Setup completed

5.使用pod

witsystemdeMacBook-Pro:~ witsystem$ pod search AFNetworking
Creating search index for spec repo 'master'.. Done!

第一次搜索时,会提示上面的信息,不过稍等片刻,就会得到你想要的效果

-> AFNetworking (3.2.0)    A delightful iOS and OS X networking framework.pod 'AFNetworking', '~> 3.2.0'- Homepage: https://github.com/AFNetworking/AFNetworking    - Source: https://github.com/AFNetworking/AFNetworking.git    - Versions: 3.2.0, 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3,    3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2,2.5.1, 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0,2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4,1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2,1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]- Subspecs:      - AFNetworking/Serialization (3.2.0)      - AFNetworking/Security (3.2.0)      - AFNetworking/Reachability (3.2.0)      - AFNetworking/NSURLSession (3.2.0)      - AFNetworking/UIKit (3.2.0) 
-> AFNetworking+AutoRetry (0.0.5)    Auto Retries for AFNetworking requestspod 'AFNetworking+AutoRetry', '~> 0.0.5'- Homepage: https://github.com/shaioz/AFNetworking-AutoRetry :q

弹出这样的信息,如果我们想退出,按键盘q即可。

常见问题

1)[!] Unable to find a pod with name, author, summary, or description matching AFNetworking

这个可能是因为你之前使用过这个命令,导致搜索结果缓存了起来,使用如下命令解决:

witsystemdeMacBook-Pro:~ witsystem$ rm ~/Library/Caches/CocoaPods/search_index.json

2)ld: warning: directory not found for option ‘-L/Users/witsystem/Library/Developer/Xcode/DerivedData/Demo-geavzrxmwgqvnxdeltiyfflyxaye/Build/Products/Debug-iphonesimulator/PGPickerView’
ld: library not found for -lPGPickerView
clang: error: linker command failed with exit code 1 (use -v to see invocation)
cocoapods1.4.0版本与xcode9.2版本一起使用有问题一直提示上面的问题,我初次使用这个,没有仔细去研究,后来更换cocoapods1.3.1就不报上面的错误了,真是太坑了,但是没完。引入第二个框架就开始报错了。
解决办法:
第一步:
步骤1
第二步:
步骤二
第三步:
步骤三
第四步:
步骤四
第五步:
步骤五
第六步:清理项目重新编译即可,不报错了吧。

3)问题3

[!] CocoaPods could not find compatible versions for pod "PGDatePicker":In Podfile:PGDatePicker (~> 2.2.1)None of your spec sources contain a spec satisfying the dependency: `PGDatePicker (~> 2.2.1)`.You have either:* out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.* mistyped the name or version.* not added the source repo that hosts the Podspec to your Podfile.Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.

解决方式:

使用命令:
pod repo update

稍等一会,即可。

  相关解决方案