前回までのあらすじ
前回: http://qiita.com/bannzai/items/ee075bd5d651a5a24d68
今回はCocoaPods
を使用したプロジェクトの時にどうすればいいか解説していきます
Swift PlaygroundsでXcode projectのコードを動かしてみよう ~~ CocoaPods ~~
まずCocoaPods
経由でライブラリをXcode project
に入れましょう
なんでもいいのですが、僕が趣味で作ったKaeru
というライブラリを使って紹介していきます
過去にQiita
でも紹介したことがあります
GitHubへのリンクです
スターください
https://github.com/bannzai/Kaeru
Podfile
に追記してpod install
しましょう
target 'ImportPlaygrounds' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'Kaeru'
end
...
ターミナルから
$ pod install
これでPods
ディレクトリにKaeru
が追加されていることが確認できれば成功です
次にCocoaPods
で追加したフレームワークをビルドして作成するスキームを作りましょう
Xcodeの左上のターゲットが表示されているバーをクリックし、Manage Schemes
を選択します
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F84459%2Faab9e72a-5f72-94a1-d3e6-dcc631271c49.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=fd49f7cb38e6d6ccc7f837879a69d28d)
下のような画面が表示されます
ImportPlaygrounds
と ImportPlaygourndsFramework
のShow
の項目にチェックが入っていることが確認できます
Pods-ImportPlaygrounds
にもチェックを入れましょう
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F84459%2Fdb1fb912-31cd-0db9-1f24-b9fbc280e38e.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=4acb6969dd29f7ccf63eb0e14af9a36f)
チェックを入れたらPods-ImportPlaygrounds
のビルドターゲットが追加されたと思います
Pods-ImportPlaygrounds
を選択してビルドしましょう
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F84459%2F98c93b8f-c04b-0581-3d80-39a26505cb0c.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=43b3b97885404f89e186abe6f5d0d4d6)
Pods-ImportPlaygrounds
のビルドが成功していればCocoaPods
で導入したライブラリが.playground
ファイルで使うことができます
.playground
ファイルで下記のコードを記載して実行結果を見ましょう
import UIKit
import ImportPlaygroundsFramework
import PlaygroundSupport
import Kaeru // CocoaPods経由で入っているライブラリをimport
print(HistoryNavigationController.self) // HistoryNavigationController
HistoryNavigationController
はKaeru
に含まれているクラスです
このコードの実行が成功したら.playground
からも参照できることがわかったかと思います
UI
の動きも問題ないか確認してみましょう
今度は下記のコードを.playground
に貼り付けて実行します
import UIKit
import ImportPlaygroundsFramework
import PlaygroundSupport
import Kaeru
let window = UIWindow(frame: UIScreen.main.bounds)
class LoopViewController: UIViewController {
override func loadView() {
view = UIView()
view.backgroundColor = .white
let currentPageLabel = UILabel()
do { // setup currentPageLabel
currentPageLabel.translatesAutoresizingMaskIntoConstraints = false
currentPageLabel.text = "current page: \(navigationController!.viewControllers.count)"
currentPageLabel.sizeToFit()
view.addSubview(currentPageLabel)
currentPageLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
currentPageLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
}
let nextButton = UIButton(type: .custom)
do { // setup next button
nextButton.translatesAutoresizingMaskIntoConstraints = false
nextButton.setTitle("next", for: .normal)
nextButton.setTitleColor(.black, for: .normal)
nextButton.backgroundColor = .orange
view.addSubview(nextButton)
nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
nextButton.topAnchor.constraint(equalTo: currentPageLabel.bottomAnchor, constant: 20).isActive = true
nextButton.heightAnchor.constraint(equalToConstant: 240).isActive = true
nextButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
nextButton.addTarget(self, action: #selector(nextButtonPressed), for: .touchUpInside)
}
let showHistoryButton = UIButton(type: .custom)
do { // setup show history button
showHistoryButton.translatesAutoresizingMaskIntoConstraints = false
showHistoryButton.setTitle("show history", for: .normal)
showHistoryButton.setTitleColor(.black, for: .normal)
showHistoryButton.backgroundColor = .brown
view.addSubview(showHistoryButton)
showHistoryButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
showHistoryButton.topAnchor.constraint(equalTo: nextButton.bottomAnchor, constant: 20).isActive = true
showHistoryButton.heightAnchor.constraint(equalToConstant: 240).isActive = true
showHistoryButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
showHistoryButton.addTarget(self, action: #selector(showHistoryButtonPressed), for: .touchUpInside)
}
}
func nextButtonPressed() {
navigationController?.pushViewController(LoopViewController(), animated: true)
}
func showHistoryButtonPressed() {
guard let navigationController = navigationController as? HistoryNavigationController else {
fatalError()
}
navigationController.presentHistory()
}
}
let viewController = LoopViewController()
let navigationController = HistoryNavigationController(rootViewController: viewController)
window.rootViewController = navigationController
window.makeKeyAndVisible()
PlaygroundPage.current.liveView = window
Playgrounds
上でコードを実行してボタンとか押してみましょう
UI
においても正常に動作することが確認できたと思います
![](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F84459%2F8be93cc5-a535-125a-48f6-e44a6caa8409.gif?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=188829bb5381434efe611dfbd4c007ad)
まとめ
**Swift PlaygroundsでXcode projectのコードを動かしてみよう ~~ CocoaPods ~~**
ということでCocoaPods
のライブラリを使いたい場合の方法をまとめてみました
外部ライブラリに依存するような画面もPlaygrounds
上で確認することができます
UI
においても毎回ビルドして確認するよりも素早く開発ができるのではないでしょうか
再掲ですがKaeru
のリンクを貼っておきますね スターください
https://github.com/bannzai/Kaeru
おしまい\(^o^)/