第 33 章
在 Swift 專案中使用 CocoaPods
了解 CocoaPods 這個 Swift 與 Objective C 專案的依賴件管理工具(dependency manager),是iOS開發者所必備的一個很重要的技術。倘若你沒有使用過 CocoaPods 的經驗,這篇文章正好就是為您準備的。
我們準備要來看什麼是 CocoaPods,了解為何你需要使用它,並知道如何以 CocoaPods 來設定專案。
什麼是CocoaPods?
首先最重要的是,什麼是 CocoaPods?CocoaPods 是針對你的 Xcode 專案的依賴件管理工具,它協助開發者管理在任何 Xcode 專案中的函式庫依賴件。
你的專案的依賴件是以一個稱作 Podfile 的文字檔來定義,CocoaPods 將會處理函式庫間的依賴件,取得最後的原始碼,然後在 Xcode 專案中將它們串連在一起來建構你的專案。
你可能會很困惑,這代表什麼意思,我們舉例來說明:
假設你想要整合你的 App 與 Google AdMob 來營利。AdMob 使用 Google Mobile Ads SDK (現在是Firebase SDK 的一部分)。要在 App 播放廣告,首先,你先要在你的 Xcode 專案安裝這個 SDK。
以前整合這個 SDK 的做法是從 Google 下載 Google Mobile Ads SDK,然後手動安裝至你的 Xcode 專案中。這樣的做法相對是比較複雜的,而且這個 SDK也要依照其他框架的搭配才能正常運作。我們來看一下由Google所提供的手動安裝程序:
- Find the desired SDK in the list.
- Make sure you have an Xcode project open in Xcode.
- In Xcode, hit
⌘-1
to open the Project Navigator pane. It will open on left side of the Xcode window if it wasn't already open.- Drag each framework from the "Analytics" directory into the Project Navigator pane. In the dialog box that appears, make sure the target you want the framework to be added to has a checkmark next to it, and that you've selected "Copy items if needed". If you already have Firebase frameworks in your project, make sure that you replace them with the new versions.
- Drag each framework from the directory named after the SDK into the Project Navigator pane. Note that there may be no additional frameworks, in which case this directory will be empty. For instance, if you want the Database SDK, look in the Database folder for the required frameworks. In the dialog box that appears, make sure the target you want this framework to be added to has a checkmark next to it, and that you've selected "Copy items if needed."
- If the SDK has resources, go into the Resources folders, which will be in the SDK folder. Drag all of those resources into the Project Navigator, just like the frameworks, again making sure that the target you want to add these resources to has a checkmark next to it, and that you've selected "Copy items if needed".
- Add the -ObjC flag to "Other Linker Settings": a. In your project settings, open the Settings panel for your target b. Go to the Build Settings tab and find the "Other Linking Flags" setting in the Linking section. c. Double-click the setting, click the '+' button, and add "-ObjC"
- Drag the
Firebase.h
header in this directory into your project. This will allow you to#import "Firebase.h"
and start using any Firebase SDK that you have.- If you're using Swift, or you want to use modules, drag module.modulemap into your project and update your User Header Search Paths to contain the directory that contains your module map.
- You're done! Compile your target and start using Firebase.
CocoaPods 是依賴件管理工具,可以讓你節省以上所有手動的程序。這歸功於一個稱作 PodFile 的文字檔。倘若你使用CocoaPods 來安裝 Google Mobile Ads SDK ,你只需要以如下的內容,在 Xcode 專案下建立一個 PodFile即可:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target 'BannerExample' do
pod 'Firebase/Core'
pod 'Firebase/AdMob'
end
當你執行 pod install
指令,CocoaPods 將為下載並安裝特定的函式庫/依賴件給你。
這也是為何 CocoaPod 能夠存在的原因,它簡化了所有的過程,自動並安裝這些框架或所需的依賴件。待會你將馬上體驗 CocoaPods 的威力。
本文摘自《iOS 17 App程式設計進階攻略》一書。如果你想繼續閱讀和下載完整程式碼,你可以從AppCoda網站購買完整電子版,全書範例檔皆可下載。