CocoaPodsを利用してiOSのライブラリを導入してみる

ハマったこと含めて紹介







CocoaPodsとは


iOS・Macアプリを作成する際ライブラリを管理してくれる便利ツール
組み込むだけなら手動でいいんだけど、更新もできるのところがいい!



CocoaPodsをインストール


前提条件としてRubyがインストールされていること
sudo gem install cocoapods
pod setup

cocoapodsのインストールに失敗する場合Rubyを最新にする
sudo gem update --system



CocoaPodsを使用してライブラリ導入


ライブラリを導入したいプロジェクトのディレクトリへ移動
XXXX.xcodeproj があるディレクトリ
プロジェクトは閉じておくこと



Podfile作成

以下のコマンドでPodfileを作成
pod init
手動でも作成できるが現在の環境に即したテンプレートが作成されるので実行したほうがいいらしい

以下が作成されたファイル
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

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

# Pods for webViewTest

target 'webViewTestTests' do
inherit! :search_paths
# Pods for testing
end

target 'webViewTestUITests' do
inherit! :search_paths
# Pods for testing
end

end



Podfileへ組み込むライブラリ向けに追記

今回はLUKeychainAccessを入れてみる
https://github.com/TheLevelUp/LUKeychainAccess

pod 'LUKeychainAccess' を追記
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

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

# Pods for webViewTest
pod 'LUKeychainAccess'

target 'webViewTestTests' do
inherit! :search_paths
# Pods for testing
end

target 'webViewTestUITests' do
inherit! :search_paths
# Pods for testing
end

end

バージョン指定なども可能
詳細は以下
http://dev.digitrick.us/notes/PodfileSyntax

podでインストールできるライブラリ一覧は以下
ライブラリ開発者が追加しているのだろうか?
https://github.com/CocoaPods/Specs/tree/master/Specs



Podfileの記述に従ってライブラリを自プロジェクトへ組み込み


以下のコマンドでライブラリをプロジェクトに組み込む

pod install

失敗する

[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master`.
You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.

http://qiita.com/pugiemonn/items/3477bc841b1044c0c8a0
cocoapodsに何か追加するらしい

cd ~/.cocoapods/repos
git clone https://github.com/CocoaPods/Specs.git master

失敗する


xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist, use `sudo xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools (or see `man xcode-select`)
コマンドラインツールがない?

Xcode からコマンドラインツールをダウンロード

Xcode → Open Developer Tool → More Developer Tools
AppleIDが必要

git clone https://github.com/CocoaPods/Specs.git master

失敗する
よくみるとパスが違う(複数バージョンのXcodeを使っている関係でリネームしていた)

以下で現在のパスが確認できる
xcode-select --print-path
/Applications/Xcode.app/Contents/Developer

自分の環境に合わせてパスをスイッチする(sudoが必要)
sudo xcode -s /Applications/Xcode8.0.app/Contents/Developer

3度目の正直

git clone https://github.com/CocoaPods/Specs.git master

OK
多少時間がかかる
クローンして気づいたけどPodfileで直接指定できるかも

プロジェクトのディレクトリへ戻って再度インストール
pod install


XXXX.xcworkspace を起動する。

cocopods


自プロジェクトへ組み込んだライブラリを使用する

Bridging-Header を作成・設定する
[ProjectName]-Bridging-Header.h で作成する。
Build Settings → Objective-C Bridging-Header → パスの設定する

Bridging-Header

$(SRCROOT)/$(PROJECT_NAME)-Bridging-Header.h
とするとどこでも使えてエラーが発生したりしないので便利


あとは一度ビルドしたあと利用したいソースでimportを記述するだけ。
Bridging-Header.h への記述はいらなかった。
import LUKeychainAccess

let keyChain = LUKeychainAccess()
keyChain.setString("てすと", forKey: "id")
let id = keyChain.string(forKey: "id")
print(id)


CocoaPodsについて詳細が知りたい場合は以下から
https://cocoapods.org/


2016年10月28日金曜日