はじめに
今回はMacにCocoaPodsを設定し、SVProgressHUDを使ってライブラリを使うところまでをこの記事では行います。
開発環境
OSX 10.11.3 (El Capitan) and macOS 10.12.4 (Sierra) and macOS 10.15.4 (Catalina)
Xcode 7.2.1 and Xcode 8.3.1 and Xcode 11.4.1
CocoaPods 0.39 and CocoaPods 1.2.0 and Cocoapods 1.9.1
CocoaPodsの環境構築
CocoaPodsを設定します。
ターミナルで以下のコマンドを打ってください。
sudo gem install cocoapods
上記のコマンドが使えない方で、OSXがEl Capitan以降の方は、ターミナルで以下のコマンドを打ってください。
※Catalinaではsudo gem install cocoapods
でいけました
sudo gem install -n /usr/local/bin cocoapods
理由はこちらから
MacOSX El Capitanでcocoapodsインストールが出来ない時の対処法
次に、インストールが終わった段階で、以下のコマンドを打ちます。
pod setup
以上で完了です!!
ライブラリの導入
ターミナルからCocoaPodsを使って、プロジェクトにライブラリを導入します。
まずターミナルから、以下の3つのコマンドを使ってプロジェクトファイルまで移動しましょう。
cd
//ディレクトリ移動
ls
//ファイルを表示
pwd
//現在のディレクトリを表示
詳しくはこちらから
今さら聞けない!ターミナルの使い方【初心者向け】
Podfileを生成
プロジェクトファイルを選択後、以下のコマンドを打ちます。
pod init
pod init
はかならず行ってください。
理由はこちらから
Cocoapods 1.0.0で注意すること
このコマンドでPodfileが自動的につくられます。
生成されたPodfileは、初期状態は以下の状態です。
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'sample-cocoapods' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for sample-cocoapods
target 'sample-cocoapodsTests' do
inherit! :search_paths
# Pods for testing
end
target 'sample-cocoapodsUITests' do
# Pods for testing
end
end
Cocoapodsのバージョンが1.0.0の場合の、初期状態は以下のようになっています
target 'アプリ名' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for SampleLocation
target 'アプリ名Tests' do
inherit! :search_paths
# Pods for testing
end
target 'アプリ名UITests' do
inherit! :search_paths
# Pods for testing
end
end
この状態から4行目のuse_frameworks!
のコメントを取ります。
Cocoapodsのバージョンが1.0.0の場合は、use_frameworks!
のコメントがすでに取れています。
これにより、Objective-Cにより作成されたライブラリでも、Bridging-Headerを作成する必要がなくなり、Swiftにより作成されたライブラリの場合は、コメントを取るのが必須なので、どちらにしても、#を消して有効化することをオススメします。
理由はこちら
Swift 対応版 CocoaPods を使う
Podfileを編集
今回はSVProgressHUDを使用しますので、以下のように編集します。
# Uncomment this line to define a global platform for your project
# platform :ios, '8.0'
# Uncomment this line if you're using Swift
use_frameworks!
target 'アプリ名' do
pod 'SVProgressHUD'
end
target 'アプリ名Tests' do
end
target 'アプリ名UITests' do
end
Cocoapodsのバージョンが1.0.0の場合は以下のように編集
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
target 'アプリ名' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for SampleLocation
pod 'SVProgressHUD'
target 'アプリ名Tests' do
inherit! :search_paths
# Pods for testing
end
target 'アプリ名UITests' do
inherit! :search_paths
# Pods for testing
end
end
ライブラリのバージョンを指定したい場合は、
pod 'SVProgressHUD', '~> 1.1.3'
バージョン管理の書き方はこちら
Cocoapodのバージョン管理
ライブラリをインストール
初めてプロジェクトにライブラリを導入するときは、
pod install
2回目以降(ライブラリの追加や削除)は、
pod update
というコマンドを打ちます。
コードの記入
次に、ローダーを表示したい画面のViewController.swiftで
まず、ライブラリをインポートします
import SVProgressHUD
あとは、必要な箇所に
SVProgressHUD.show()
などと記述するだけです!
例えば、ViewController.swiftが表示されると同時に、ローディングを表示する場合は、以下のように書いていただければ、大丈夫です。
import UIKit
import SVProgressHUD // インポートは必ずここ
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
SVProgressHUD.show()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}