前回までのあらすじ
前回: 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を選択します
下のような画面が表示されます
ImportPlaygrounds と ImportPlaygourndsFrameworkのShowの項目にチェックが入っていることが確認できます
Pods-ImportPlaygroundsにもチェックを入れましょう
チェックを入れたらPods-ImportPlaygroundsのビルドターゲットが追加されたと思います
Pods-ImportPlaygroundsを選択してビルドしましょう
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においても正常に動作することが確認できたと思います
まとめ
**Swift PlaygroundsでXcode projectのコードを動かしてみよう ~~ CocoaPods ~~**
ということでCocoaPodsのライブラリを使いたい場合の方法をまとめてみました
外部ライブラリに依存するような画面もPlaygrounds上で確認することができます
UIにおいても毎回ビルドして確認するよりも素早く開発ができるのではないでしょうか
再掲ですがKaeruのリンクを貼っておきますね スターください
https://github.com/bannzai/Kaeru
おしまい\(^o^)/