LoginSignup
31
20

More than 5 years have passed since last update.

Cocoapods と Carthage を共存させた iOS プロジェクトの始め方

Last updated at Posted at 2018-06-10

Cocoapods と Carthage

iOS 開発でのライブラリ管理といえば Cocoapods が主流だが
ビルド時間を短縮できるなどのメリットから
Carthage 対応しているものに関しては Carthage で管理したい

環境

Mac で Homebrew は導入済みとする

  • Git
  • rbenv
  • bundler
  • Cocoapods
  • SwiftLint
  • Carthage
  • RxSwift

を利用した iOS プロジェクトの始め方を備忘録的にメモ

プロジェクト作成

Git リポジトリと Xcode プロジェクトを作成
どんな方法でもいいがディレクトリは入れ子にならない方が良い

SamplApp
├── .git
├── README.md
├── SampleApp
├── SampleApp.xcodeproj
├── SampleAppTests
└── SampleAppUITests

gitignore の設定

GitHub のテンプレートを利用して .gitignore を作成する
https://github.com/github/gitignore/blob/master/Swift.gitignore

Cocoapods と Carthage による成果物を git の管理外にする

ただ、再ビルドを不要にするためにバージョン管理に含めてしまうのもあり
CocoaPods・Carthageでインストールした成果物はバージョン管理に含めるべきか?

.gitignore
~ 中略 ~

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
Pods/
vendor/bundle

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
Carthage/Checkouts
Carthage/Build

~ 中略 ~

Cocoapods の設定

[参考]
MacにHomeBrew,rbenv,bundlerをインストールする
bundler で cocoapods そのもののバージョン管理をする

rbenv のインストール

ruby のバージョンを管理するために rbenv を導入する

$ brew install rbenv

ruby の path を設定するために .bash_profile に書き込む

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

rbenv で ruby の version を管理
(LTS ならなんでもいい)

$ source ~/.bash_profile
$ rbenv version
$ rbenv versions
$ ruby -v
$ rbenv install 2.5.1
$ rbenv global 2.5.1
$ rbenv rehash
$ rbenv versions
$ ruby -v

bundler のインストール

gem をローカルで管理するためのツール (Cocoapods のバージョン管理のために必要)
https://github.com/bundler/bundler

bundler だけ global で管理

$ gem install bundler

確認する

$ bundler -v

Cocoapods のインストール

プロジェクトのルートディレクトリに移動して bundler の初期化

$ bundler init

生成された Gemfile を編集 (Cocoapods のバージョンは必要に応じて上げる)
https://github.com/CocoaPods/CocoaPods/releases

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

# git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"
gem 'cocoapods' , '1.5.3'

Cocoapods のインストール

$ bundle install --path vendor/bundle

確認して初期設定 (bundler を通してインストールした gem は bundle exec をつけて実行する)

$ bundle exec pod --version
$ bundle exec pod setup

Podfile の作成

$ bundle exec pod init

Podfile を編集
ここでは例として swift 用 lint ツールの SwiftLint を導入する

Podfile
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

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

# Pods for SampleApp
pod 'SwiftLint', '~> 0.25'

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

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

end

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

$ bundle exec pod install

以下を参考に Xcode で SwiftLint を使うための設定を行う (本記事では割愛)

https://github.com/realm/SwiftLint
【SwiftLint】CocoaPodsでSwiftLintを導入する

  • line_length
  • trailing_whitespace
  • vertical_whitespace

あたりを適切に設定しておかないと Xcode が自動生成するファイルも引っかかるので注意
参考までに .swiftlint.yml の例を載せる

.swiftlint.yml
# 無効にするルール
disabled_rules:
  - trailing_whitespace
  - vertical_whitespace

# デフォルト無効で有効にするルール
opt_in_rules:
  - conditional_returns_on_newline
  - empty_count
  - missing_docs
  - operator_usage_whitespace

# 対象外のファイル・フォルダ
excluded:
  - Pods/
  - Carthage/

# 1行あたりの文字数制限を300に変更
# プロジェクト作成時にデフォルトで追加されるコメントをひっかけないため
line_length: 300

# 変数名が1文字以上なら許可に変更
identifier_name:
  min_length: 1

以後 Xcode を開くときは SampleApp.xcodeproj ではなく SampleApp.xcworkspace を開くこと
Build を行って動作確認

Carthage の設定

[参考]
Carthage について

Homebrew でインストール

$ brew install carthage

pod init 的なものはおそらく無いので手動で Cartfile を作成

$ touch Cartfile

Cartfile を編集
今回は RxSwift を導入する

Cartfile
github "ReactiveX/RxSwift" ~> 4.0

ライブラリをインストールしてビルドする

$ charthage bootstrap --platform iOS

Xcode の設定を行う (割愛)
Framework を追加するときは「Add Other...」から Carthage -> Build -> iOS -> ***.framework を選ぶこと

これでもう一度 Build すればライブラリが import できるようになっているはず

ディレクトリ構成

こんな感じになっていれば OK

SampleApp
├── .bundle/
├── .git/
├── .gitignore
├── .swiftlint.yml
├── Cartfile
├── Cartfile.resolved
├── Carthage
│   ├── Build/
│   └── Checkouts/
├── Gemfile
├── Gemfile.lock
├── SampleApp/
├── SampleApp.xcodeproj
├── SampleApp.xcworkspace
├── SampleAppTests/
├── SampleAppUITests/
├── Podfile
├── Podfile.lock
├── Pods/
├── README.md
└── vendor
    └── bundle/
31
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
20