補習ほぼ確

学びや好きなことをただ自由に書く

CocoaPods で Chameleon Framework を使ってみる

毎回 CocoaPods 導入手順を調べている&Swift4で動作確認済みだよと明記されてる記事意外と無いので、一通りの流れをまとめる自分用のmemoがてら

今回はフラットデザインに使えるカラースキームが自在に取得&設定できたり、テーマの定義がいい感じにできちゃうこちらの Chameleon Framework というライブラリをpods経由で導入してみる。 github.com

  • 動作確認環境
$ swift -version
Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)
Target: x86_64-apple-darwin17.5.0

$ pod --version
1.5.3

Cocoapods インストール

$ sudo gem install cocoapods

CocoaPodsのセットアップ。かなり時間が掛かった

$ pod setup
Setting up CocoaPods master repo
Performing a deep fetch of the `master` specs repo to improve future performance
  $ /usr/local/bin/git -C /Users/Hikari/.cocoapods/repos/master fetch origin --progress
  remote: Enumerating objects: 27, done.
  remote: Counting objects: 100% (27/27), done.
  remote: Compressing objects: 100% (13/13), done.
  remote: Total 18 (delta 12), reused 7 (delta 4), pack-reused 0
  From https://github.com/CocoaPods/Specs
     03e813aace0..d31082052cf  master     -> origin/master
  $ /usr/local/bin/git -C /Users/Hikari/.cocoapods/repos/master rev-parse --abbrev-ref HEAD
  master
  $ /usr/local/bin/git -C /Users/Hikari/.cocoapods/repos/master reset --hard origin/master
  Checking out files: 100% (367530/367530), done.
  HEAD is now at d31082052cf [Add] MapsIndoors 3.0.0-alpha36
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your diff.renameLimit variable to at least 224792 and retry the command.

CocoaPods 1.6.0.beta.2 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.6.0.beta.2

Setup completed
Podfile 作成

ライブラリを導入したいプロジェクトファイルのあるディレクトリに移動

$ ls -la
total 8
drwxr-xr-x   8 Hikari  staff  256 10 14 17:32 .
drwxr-xr-x   7 Hikari  staff  224 11  8 00:26 ..
drwxr-xr-x  15 Hikari  staff  480  1  4 00:19 .git
drwxr-xr-x   9 Hikari  staff  288 12 12 02:22 Mojiire
drwxr-xr-x@  5 Hikari  staff  160 12 11 01:35 Mojiire.xcodeproj 
drwxr-xr-x   4 Hikari  staff  128  8 19 03:12 MojiireTests
drwxr-xr-x   4 Hikari  staff  128 10 14 17:14 MojiireUITests
-rw-r--r--   1 Hikari  staff   10 10 14 17:32 README.md

ここで pod init を叩くと Podfile が作成される。
(以前は気にしたこと無かったけど、Cocoapods は Ruby 製なので Podfile もXcodeから確認するとRubyのファイルとして認識される)

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

target '{プロジェクトファイル名}' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!
  1 # Uncomment the next line to define a global platform for your project

  # Pods for Mojiire

  target '{プロジェクトファイル名}Tests' do
    inherit! :search_paths
    # Pods for testing
  end

  target '{プロジェクトファイル名}UITests' do
    inherit! :search_paths
    # Pods for testing
  end

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

上記Podfileの10行目あたりに以下を追記。

pod 'ChameleonFramework/Swift', :git => 'https://github.com/ViccAlexander/Chameleon.git'

pod install を実行。

$ pod install
Analyzing dependencies
Pre-downloading: `ChameleonFramework` from `https://github.com/ViccAlexander/Chameleon.git`
Downloading dependencies
Installing ChameleonFramework (2.1.0)
Generating Pods project
Integrating client project

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

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

プロジェクトファイルと同じ階層にCocoaPods関連のファイルが作成される。git管理してるとわかりやすい。

$ git status
On branch add-chameleon
Your branch is up to date with 'GitHub/add-chameleon'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   Mojiire.xcodeproj/project.pbxproj
    modified:   Mojiire.xcodeproj/xcuserdata/Hikari.xcuserdatad/xcschemes/xcschememanagement.plist

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    Mojiire.xcworkspace/
    Podfile
    Podfile.lock
    Pods/

no changes added to commit (use "git add" and/or "git commit -a")

また初回インストール以降新たにライブラリを定義していく場合は pod update を実行する。

ライブラリを利用してみる

pod install 完了後、{プロジェクト名}.xcworkspace (CocoaPods のワークスペース)を開き、
利用したいファイルで import 宣言する

import ChameleonFramework

Cannot load underlying module というエラーが表示されたら、 Product > Clean でプロジェクトをcleanする。

Cameleon で定義されているフラットカラーを使ってみる。

let pinkColor = UIColor.flatPink
let coffeeColor = UIColor.flatCoffee

UIColor.f... の時点でインポートした ChameleonFramework のコード補完が表示されるのが確認できる。
https://github.com/ViccAlexander/Chameleon#-product-features これでこちらに定義されているようなカラーがアプリ内で表示されているのが確認できた。

Chameleon がいったん使えるまでが今回のゴールなのでここまで。
こういういい感じのデザインがちゃっと用意できちゃうライブラリすばらしいな