Cocoapods
安裝
-
(可選)使用
taobaoruby-china 源替換默認 gem 源:gem source blabla..
$ gem sources -l *** CURRENT SOURCES *** https://rubygems.org/ $ gem sources --remove https://rubygems.org/ https://ruby.taobao.org/ removed from sources $ gem source -a https://gems.ruby-china.com/ https://gems.ruby-china.com/ added to sources $ gem source -c *** Removed specs cache *** $ gem source -u source cache successfully updated $ gem sources -l *** CURRENT SOURCES *** https://gems.ruby-china.com/
sudo gem install cocoapods
-
(可選)切換 pod 源
$ pod repo master - Type: git (master) - URL: https://github.com/CocoaPods/Specs.git - Path: /Users/qiwihui/.cocoapods/repos/master
$ pod repo remove master
$ pod repo add master https://git.coding.net/CocoaPods/Specs.git
$ pod repo update
$ pod setup
```
或者
```bash
$ git clone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master
$ pod repo update
```
切換回官方鏡像
```bash
$ pod repo remove master
$ pod repo add master https://github.com/CocoaPods/Specs.git
$ pod repo update
Updating spec repo `master`
$ /usr/local/bin/git -C /Users/qiwihui/.cocoapods/repos/master fetch origin --progress
remote: Enumerating objects: 511, done.
remote: Counting objects: 100% (511/511), done.
remote: Compressing objects: 100% (134/134), done.
remote: Total 820 (delta 399), reused 449 (delta 367), pack-reused 309
Receiving objects: 100% (820/820), 99.24 KiB | 401.00 KiB/s, done.
Resolving deltas: 100% (501/501), completed with 194 local objects.
From https://github.com/CocoaPods/Specs
5b04790953c..e3ba7ee3a29 master -> origin/master
$ /usr/local/bin/git -C /Users/qiwihui/.cocoapods/repos/master rev-parse --abbrev-ref HEAD
master
$ /usr/local/bin/git -C /Users/qiwihui/.cocoapods/repos/master reset --hard origin/master
HEAD is now at e3ba7ee3a29 [Add] IOS_OC_BASIC 6.3
CocoaPods 1.6.0.beta.2 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.6.0.beta.2
```
-
如果Podfile文件中有
source 'https://github.com/CocoaPods/Specs.git'
也需要把它換成repo的源,否則依然是使用GitHub源
基礎用法
cd <project_folder>
pod init
-
編輯 Podfile, example
# 平臺,必需 platform :ios, '9.0' # 隱藏警告 inhibit_all_warnings! target 'AlamofireDemo' do # Using Swift and want to use dynamic frameworks use_frameworks! # 項目 Pods pod 'Alamofire', '~> 4.5' target 'AlamofireDemoTests' do inherit! :search_paths # 測試 Pods end end
版本支持:
- `>`, `>=`, `<`, `<=` - `~>`: **up to** next major | minor | patch - `:path` 本地絕對路徑 - `:git` git項目地址,還可使用 `:branch`, `:tag`, `:commit`
pod install
- Always 打開項目下 *.xcworkspace 文件作爲項目入口
pod install 和 pod update 區別
-
pod install [package_name]
: 安裝特定版本的 pods -
pod update [package_name]
: 升級 pods 到最新版本
Carthage
安裝
brew install carthage
使用
-
編輯
Cartfile
,比如SwiftyJSON
github "SwiftyJSON/SwiftyJSON"
-
carthage update [--platform ios]
$ carthage update *** Fetching SwiftyJSON *** Checking out SwiftyJSON at "4.2.0" *** xcodebuild output can be found in /var/folders/kl/g94q0k_571vdjtcwzzcv20s40000gn/T/carthage-xcodebuild.nN22hg.log *** Building scheme "SwiftyJSON iOS" in SwiftyJSON.xcworkspace *** Building scheme "SwiftyJSON watchOS" in SwiftyJSON.xcworkspace *** Building scheme "SwiftyJSON tvOS" in SwiftyJSON.xcworkspace *** Building scheme "SwiftyJSON macOS" in SwiftyJSON.xcworkspace
-
Carthage
目錄下:- Build(編譯出來的.framework二進制代碼庫)
- Checkouts(源碼)
$ tree -L 3 Carthage/ Carthage/ ├── Build │ ├── Mac │ │ ├── SwiftyJSON.framework │ │ └── SwiftyJSON.framework.dSYM │ ├── iOS │ │ ├── 22BD4B6C-0B26-35E1-AF5F-8FB6AEBFD2FD.bcsymbolmap │ │ ├── C862E8A1-24ED-398A-A8E9-A7384E34EDB1.bcsymbolmap │ │ ├── SwiftyJSON.framework │ │ └── SwiftyJSON.framework.dSYM │ ├── tvOS │ │ ├── 1ADB9C1F-36CA-3386-BF07-6EE29B5F8081.bcsymbolmap │ │ ├── SwiftyJSON.framework │ │ └── SwiftyJSON.framework.dSYM │ └── watchOS │ ├── A8A151AB-D15E-3A0B-8A17-BF1A39EC6AB4.bcsymbolmap │ ├── EA427A42-6D21-3FF4-919F-5E50BF8A5D7B.bcsymbolmap │ ├── SwiftyJSON.framework │ └── SwiftyJSON.framework.dSYM └── Checkouts └── SwiftyJSON ├── CHANGELOG.md ├── Example ├── LICENSE ├── Package.swift ├── README.md ├── Source ├── SwiftyJSON.podspec ├── SwiftyJSON.xcodeproj ├── SwiftyJSON.xcworkspace ├── Tests └── scripts
- 添加生成的文件: 項目 "General" -> "Linked Frameworks and Libraries" -> 將
Carthage/Build/iOS
中的.framework
文件添加到項目中 -
"Build Phases" -> "+" -> "New Run Script Phase"
- /bin/sh
- /usr/local/bin/carthage copy-frameworks
- "Input Files": $(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework
- "Output Files": $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/SwiftyJSON.framework
添加這個 Run Script 的作用是爲了讓運行時能夠找到這個動態庫。
還可以將 Carthage 所集成的第三方庫生成的符號文件添加到項目中,這樣我們在調試的時候,就可以步入第三方庫內部的代碼:`Build Phrases` -> `New Copy Files Phrase`,將 Carthage/Build/iOS 目錄中的 `SwiftyJSON.framework.dSYM` 符號文件拖動進來
Carthage 和 CoaoaPods 的區別
CoaoaPods
是一套整體解決方案,我們在 Podfile
中指定好我們需要的第三方庫。然後 CocoaPods
就會進行下載,集成,然後修改或者創建我們項目的 workspace
文件,這一系列整體操作。
相比之下,Carthage
就要輕量很多,它也會一個叫做 Cartfile
描述文件,但 Carthage
不會對我們的項目結構進行任何修改,更不多創建 workspace
。它只是根據我們描述文件中配置的第三方庫,將他們下載到本地,然後使用 xcodebuild
構建成 framework
文件。然後由我們自己將這些庫集成到項目中。Carthage
使用的是一種非侵入性的哲學。
另外 Carthage
除了非侵入性,它還是去中心化的,它的包管理不像 CocoaPods
那樣,有一箇中心服務器(cocoapods.org),來管理各個包的元信息,而是依賴於每個第三方庫自己的源地址,比如 Github。