CocoaPodsのインストールと使い方

久しぶりにpod –versionをしてみたら、エラーらしきメッセージが出てきたので、再度CocoaPodsをインストールしてみようと思った。

インストールと初期セットアップ

まずは、gem(パッケージ管理ツール)を使ってインストールを試みるも失敗。

notsukunnoMacBook-Pro-10:~ nokkun$ sudo gem install cocoapods
Password:
Building native extensions. This could take a while...
ERROR:  Error installing cocoapods:
	ERROR: Failed to build gem native extension.

調べてみると、gemではなくbrewで入れるのが良いみたい。

https://teratail.com/questions/245100

notsukunnoMacBook-Pro-10:~ nokkun$ brew install cocoapods

以下のメッセージが。

Target /usr/local/bin/pod
already exists. You may want to remove it:
  rm '/usr/local/bin/pod'

To force the link and overwrite all conflicting files:
  brew link --overwrite cocoapods

すでにpodが存在するから、上書きする?と聞かれている。

言われた通りにコマンドを叩くとうまくいった模様。

notsukunnoMacBook-Pro-10:~ nokkun$ brew link --overwrite cocoapods
Linking /usr/local/Cellar/cocoapods/1.11.3... 2 symlinks created.
notsukunnoMacBook-Pro-10:~ nokkun$ pod --version
1.11.3

最後にセットアップをします。

notsukunnoMacBook-Pro-10:~ nokkun$ pod setup
Setup completed

ライブラリのインストール

プロジェクトファイルに移動して、initを打ちます。

XcodeではデフォルトでDocumentの下にプロジェクトファイルが作成されています。

notsukunnoMacBook-Pro-10:Documents nokkun$ cd ScrapingAmazonAssociate/
notsukunnoMacBook-Pro-10:ScrapingAmazonAssociate nokkun$ pod init

すると以下のファイルが生成されます。

notsukunnoMacBook-Pro-10:ScrapingAmazonAssociate nokkun$ cat Podfile 
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'ScrapingAmazonAssociate' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for ScrapingAmazonAssociate

end

今回インストールするライブラリSwiftSoupをnanoを使って記載します。

notsukunnoMacBook-Pro-10:ScrapingAmazonAssociate nokkun$ cat Podfile 
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'ScrapingAmazonAssociate' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for ScrapingAmazonAssociate
  pod 'SwiftSoup'
end

インストールします。

notsukunnoMacBook-Pro-10:ScrapingAmazonAssociate nokkun$ pod install
Analyzing dependencies
Downloading dependencies
Installing SwiftSoup (2.4.3)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `ScrapingAmazonAssociate.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform `iOS` with version `15.2` on target `ScrapingAmazonAssociate` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

ライブラリが組み込まれたプロジェクトxcwordspaceが作成されました!

ABOUTこの記事をかいた人

個人アプリ開発者。Python、Swift、Unityのことを発信します。月間2.5万PVブログ運営。 Twitter:@yamagablog