前言

為了突破臉書的時間軸演算法,我著手開發 iOS 應用程式,暫定名為 PageFeed。

透過個別訪問粉專並解析 HTML 檔來獲取粉專的最新動態。

其中,解析 HTML 需要一款名為 Kanna 的類庫支援,遂稍微研究了 CocoaPods 的應用。


關於 CocoaPods

CocoaPods 是一應用級別的依賴管理工具,支援 Swift 和 Objective-C 程式開發。

幫助輕鬆管理 Xcode 項目,只需要透過簡單的指令就能管理類庫項目,節省大量配置和部署的時間。

若沒有依賴管理工具協助,使用類庫便需要逐一下載並加入 Xcode 項目中。

但當專案規模增大,要處理的類庫數目增加,逐一手動新增或更新絕對是費時失事之舉。


實作過程

安裝 CocoaPods

於終端機下指令,安裝 CocoaPods:

sudo gem install cocoapods

初始化 CocoaPods

新增 Xcode 專案,cd到專案目錄,於終端機下指令,初始化 CocoaPods:

pod init

便會生成 Podfile,如下:

# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'

target 'PageFeed' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for PageFeed

end

添加 Kanna 類庫

修改 Podfile 以添加 Kanna 類庫:

# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'

target 'PageFeed' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for PageFeed
  pod 'Kanna', '~> 5.0.0'
end

安裝類庫

cd到專案目錄,於終端機下指令,下載安裝類庫:

pod install

類庫安裝完成後,將建立.xcworkspace檔案。

往後改由開啟.xcworkspace以啟動專案,而非.xcodeproj


使用類庫

.swift檔案中添加import Kanna即可使用 Kanna 類庫。