关于CocoaPods,是OS X和iOS下的一个第三方类库管理工具,它的好处这里不多说,主要说下如何安装CocoaPods。
CocoaPods的安装需要Ruby环境,Mac系统都自带Ruby,通过下面终端命令查看Ruby版本:
ruby -v
~$ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
CocoaPods支持的Ruby最低版本是2.2.2,如果自己的版本低于2.2.2就需要更新升级Ruby:
sudo gem update --system
更换Ruby镜像
Ruby默认的源地址是国外网络地址,通过下面终端命令查看当前镜像地址:
gem sources -l
~$gem sources -l
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
*** CURRENT SOURCES ***
https://rubygems.org/
首先移除当前镜像
gem sources --remove https://rubygems.org/
~$gem sources --remove https://rubygems.org/
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
https://rubygems.org/ removed from sources
然后添加国内最新Ruby镜像地址
gem sources -a https://gems.ruby-china.com/
~$gem sources -a https://gems.ruby-china.com/
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
https://gems.ruby-china.com/ added to sources
这时候再查看镜像地址就是这样的了
~$gem sources -l
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
安装CocoaPods
Ruby环境安装好以后,接下来就是安装CocoaPods,终端输入:
sudo gem install cocoapods
~$sudo gem install cocoapods
Password:
......
Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, nap, fuzzy_match, cocoapods-core, claide, cocoapods-deintegrate, cocoapods-downloader, cocoapods-plugins, cocoapods-search, cocoapods-stats, netrc, cocoapods-trunk, cocoapods-try, molinillo, atomos, CFPropertyList, colored2, nanaimo, xcodeproj, escape, fourflusher, gh_inspector, ruby-macho, cocoapods after 33 seconds
28 gems installed
如果终端出现上面的提示,表示安装成功。到这里还剩最后一步,还需执行命令:
pod setup
这一步可能需要耐心等待,需要下载上百M的文件。
~$pod setup
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
Setting up CocoaPods master repo
$ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Enumerating objects: 179, done.
remote: Counting objects: 100% (179/179), done.
remote: Compressing objects: 100% (135/135), done.
remote: Total 2819986 (delta 66), reused 64 (delta 42), pack-reused 2819807
Receiving objects: 100% (2819986/2819986), 614.27 MiB | 834.00 KiB/s, done.
Resolving deltas: 100% (1676501/1676501), done.
Checking out files: 100% (304760/304760), done.
Setup completed
至此,CocoaPods安装完成!
使用CocoaPods
在使用之前,我们先验证下CocoaPods是否安装成功,搜索一个第三方库。终端输入:
~$pod search AFNetworking
正常情况下,这是会提示Creating search index for spec repo 'master'...
等待一会就会出现搜索结果了(也可能需要等待很长时间,大约二十几分钟,不要着急,耐心等待就行😂)。
出现上图这个就说明成功了。直接按“Q”键就可以退出。
接下来,终端进入iOS项目工程的根目录(就是*.xcodeproj所在的目录),然后创建Podfile文件,终端输入:
~$touch Podfile
这时工程目录下多了一个Podfile文件。
现在我们要接入Admob广告SDK,就可以使用CocosPods的方法,用编辑器打开Podfile文件,并将下面的的代码加到里面:
platform :ios, '8.0'
target 'MyApp' do
pod 'Google-Mobile-Ads-SDK'
end
其中,MyApp应该是你项目中的Target名称。文件保存后,在终端中输入~$pod install
。
安装成功后,工程目录中会生成一个*.xcworkspace文件。
以后打开工程就打开这个文件,不再使用*.xcodeproj文件。
问题报错
当我们打开*.xcworkspace文件使用Xcode编译时,可能会报这么一个错误:
diff: /../Podfile.lock: No such file or directory
diff: /Manifest.lock: No such file or directory
error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.