CocoaPods使ってみた
CocoaPodsはObjectiv-Cのパッケージ管理ツール。
RubyでいうGem、node.jsでいうnpmにあたると思う。
簡単な導入手順と感想を書く。
疑問に思ったこととかも書くので誰かの参考になれば。
インストール
$ sudo gem install cocoapods
$ pod --version 0.31.1
$ pod --help CocoaPods, the Objective-C library package manager. Commands: * help Show help for the given command. * init Generate a Podfile for the current directory. - install Install project dependencies * ipc Inter-process communication * lib Develop pods * list List pods * outdated Show outdated project dependencies * push Push new specifications to a spec-repo * repo Manage spec-repositories * search Searches for pods * setup Setup the CocoaPods environment * spec Manage pod specs * try Try a Pod! * update Update outdated project dependencies Options: --silent Show nothing --version Show the version of CocoaPods --no-ansi Show output without ANSI codes --verbose Show more debugging information --help Show help banner of specified command
なんか最初にリポジトリ情報(?)初期化するらしい。
$ pod setup Setting up CocoaPods master repo Already up-to-date. Setup completed (read-only access)
使う
Xcodeのプロジェクトディレクトリに移動してpod init
するとPodfileのひな形ができる。
RubyでいうGemfile。node.jsでいうpackage.json。
npmみたいに--saveオプションとかあればいいのに。
# プロジェクトディレクトリに移動 $ cd XcodeSampleDir # Podfileのひな形作成 $ pod init # 中身を確認 $ cat Podfile # Uncomment this line to define a global platform for your project # platform :ios, "6.0" target "XcodeSampleDir" do end
platform
はコメントアウト外してよと書かれている。
今回はiosじゃなくてmacアプリ作りたかったのでosxにする。
またバージョンはよく分からないのが今の自分のOSのバージョンを書いておく。
実際のところどのバージョン書くのが適切なんですかね?
ちなみに省略時には結構古いバージョンを指定したことになるみたい。
http://guides.cocoapods.org/syntax/podfile.html#platform
あとはPodfileに使いたいライブラリを書いていくわけだが、実際何をどう書くべきか悩む、というか面倒くさい。
pod search
すれば書くべき内容を教えてくれた。
$ pod search SSZipArchive -> SSZipArchive (0.3.1) Utility class for zipping and unzipping files on iOS and Mac. pod 'SSZipArchive', '~> 0.3.1' - Homepage: https://github.com/soffes/ssziparchive - Source: https://github.com/soffes/ssziparchive.git - Versions: 0.3.1, 0.3.0, 0.2.5, 0.2.4, 0.2.3, 0.2.2, 0.2.1, 0.2.0, 0.1.2, 0.1.1, 0.1.0 [master repo]
以下のようになった。
platform :osx, '10.9.2' target "XcodeSampleDir" do pod 'SSZipArchive', '~> 0.3.1' end
ライブラリのバージョン番号も必須ではなく、省略すると最新が入る。
詳しくはここ。
ライブラリのインストール元にはGithubリポジトリやローカルパスも指定できるみたい。
あとはpod install
する。
$ pod install Analyzing dependencies Downloading dependencies Installing SSZipArchive (0.3.1) Generating Pods project Integrating client project
実行後、ディレクトリを見るとPodfile.lockというファイルとPodsというディレクトリができている。
またプロジェクトにワークスペースがなければワークスペース(*.xcworkspace)もできている。
今回プロジェクトしか作ってなかったのでXcodeを開き直す。
これでライブラリを自作コードの中からimport出来るようになる。
#import <Foundation/Foundation.h> #import "SSZipArchive.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0; }
なんかXcodeとターミナルを行ったり来たりする感じがあって慣れなかったけど、まあないより抜群に便利な気がする。
というかもはやこういうツールないと安心してコーディングできない気すらするので良いのか悪いのか。