以前常用VS的 Nuget管理项目的外部类库与一些小插件。在xocde里有类似的东西,它就是Cocopods。

如何安装Cocopods

MACOS开发环境配置

Get Start

原文直接来自官网

上面的内容是在pods里搜索,在xcode的项目目录的一个叫Podfile的文件中列出依赖项
Search for pods (above). Then list the dependencies in a text file named Podfile in your Xcode project directory:

1
2
3
4
5
6
7
8
9
platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
pod 'AFNetworking', '~> 2.6'
pod 'ORStackView', '~> 3.0'
pod 'SwiftyJSON', '~> 2.3'
end

提示:CocoaPods提供了一个pod init命令来创建一个灵活的默认Podfile,你会用到它的。
Tip: CocoaPods provides a pod init command to create a Podfile with smart defaults. You should use it.

现在你可以在项目里安装依赖项了
Now you can install the dependencies in your project:

1
$ pod install

确保在构建项目时始终打开Xcode工作区(xcworkspace文件)而不是项目文件:
Make sure to always open the Xcode workspace instead of the project file when building your project:

1
$ open App.xcworkspace

现在您可以导入依赖关系,例如:
Now you can import your dependencies e.g.:

1
#import <Reachability/Reachability.h>

基础命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Usage:

$ pod COMMAND

CocoaPods, the Cocoa library package manager.

Commands:

+ cache Manipulate the CocoaPods cache
+ env Display pod environment
+ init Generate a Podfile for the current directory
+ install Install project dependencies according to versions from a
Podfile.lock
+ ipc Inter-process communication
+ lib Develop pods
+ list List pods
+ outdated Show outdated project dependencies
+ repo Manage spec-repositories
+ setup Setup the CocoaPods environment
+ spec Manage pod specs
+ update Update outdated project dependencies and create new
Podfile.lock

Options:

--silent Show nothing
--version Show the version of the tool
--verbose Show more debugging information
--no-ansi Show output without ANSI codes
--help Show help banner of specified command
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-1.2.0/lib/cocoapods/command.rb:47:in `run'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-1.2.0/bin/pod:55:in `<top (required)>'
from /usr/local/bin/pod:23:in `load'
from /usr/local/bin/pod:23:in `<main>'

为自己的开源项目添加pod支持

iOS开发– 创建podspec文件,为自己的项目添加pod支持

Setup报错

1
2
$ git config --global http.postBuffer 24288000
$ git config --list

若输出 http.postbuffer=24288000, 就 OK了

cocoapods:常见错误总结

可视化CocoaPods

我使用 alcatraz来可视化CocoaPods操作。因为有时候用命令行不是很方便。

安装命令如下:

1
curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | sh

卸载命令如下:

1
2
rm -rf ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin
rm -rf ~/Library/Application\ Support/Alcatraz

XCode 8.X 需要 usign 才能使用插件

解决插件升级不能使用的问题

在xcode版本更新的之后,或者从package manager新下载了插件,有时候不可用。这时候就需要找到下面目录
~/Library/Application Support/Developer/Shared/Xcode/Plug-ins
然后找到新下载的插件 ShowContent 。打开plist文件,在DVTPlugInCompatibilityUUIDs列表中新增一串ID。

下面是这串ID的获取方法:

1
defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID