看板 KnucklesNote
作者 標題 [Xcode][Swift3] 安裝套件管理工具 CocoaPods
時間 2017-03-05 Sun. 15:19:55
要使用第三方類別庫的話,要安裝套件管理工具 CocoaPods
![[圖]](http://i.imgur.com/WlGXnMY.png)
例如我們想要裝簡化網路存取功能的類別庫 Alamofire
在 Mac 安裝 CocoaPods
打開終端機,輸入以下指令
$ sudo gem install cocoapods
![[圖]](http://i.imgur.com/ecxndil.png)
CocoaPods 是使用 Ruby 語言寫的,
gem 是安裝 Ruby 程式的指令,Mac 預設就有支援 Ruby
安裝好了之後,使用 cd 切換到專案的目錄
例如專案的資料是放在 帳號的家目錄/Xcode/DispBBS 的話,輸入
$ cd ~/Xcode/DispBBS
使用 CocoaPods 的指令 pod 在專案目錄建立一個 Podfile 檔
$ pod init
使用 vim 編輯 Podfile,加上想安裝的 Alamofire
$ vim Podfile
# Uncomment this line to define a global platform for your project
platform :ios, '9.0'
target 'DispBBS' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for DispBBS
pod 'Alamofire'
end
移動輸入游標,在 # Pods for DispBBS 這行下面platform :ios, '9.0'
target 'DispBBS' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for DispBBS
pod 'Alamofire'
end
按 i 切換到 insert 模式,輸入 pod 'Alamofire'
接著按 Esc 退出 insert 模式後,輸入「:x」即可存檔離開 vim
回到專案目錄下,輸入
$ pod install
就會開始安裝寫在 Podfile 裡的函式庫
安裝好之後會在專案的目錄下新增一個 DispBBS.xcworkspace 檔
之後都要改用 DispBBS.xcworkspace 來開啟專案
而不是原本的 DispBBS.xcodeproj
回到 Xcode,關閉原本的專案,使用 DispBBS.xcworkspace 開啟
可以看到專案資料夾變成這樣
![[圖]](http://i.imgur.com/AVk2IMz.png)
Alamofire 已經加入專案中了
要使用 Alamofire 時在檔案前面加上
import Alamofire
如果出現錯誤訊息「cannot load underlying module for 'Alamofire'點一下「Product」/「Clean」,再點「Build」就可以了
![[圖]](http://i.imgur.com/ryjginp.png)
更新類別庫的版本
在專案的目錄下執行
$ pod update
查詢安全的類別庫版本
打開專案目錄下的 Podfile.lock
這邊會記錄目前安裝的類別庫版本
以及 CocoaPods 版本
指定安裝版本
要指定安裝的類別庫板本,例如 Alamofire 4.4.0
修改 Podfile 中的 pod 'Alamofire' 為
pod 'Alamofire', '~> 4.4.0'
移除類別庫
將類別庫那一行加上# 或是刪除那行
然後再執行
$ pod update
參考
AppCoda CocoaPods 簡介 : 如何輕鬆管理 Swift / Objective-C 的類庫
□ 錯誤解決記錄
pod update 後出現
[!] Failed to connect to GitHub to update the CocoaPods/Specs specs repo
參考 StackOverflow
照上面的步驟更新 openssl, ruby, cocoapod
--
※ 作者: Knuckles 時間: 2017-03-05 15:19:55
※ 編輯: Knuckles 時間: 2018-08-21 00:31:17
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 569
回列表(←)
分享