アプリを開発していて、チュートリアルをたくさんやり直して動作確認をしたかったりする時がありますよね。
そんな時はデバッグメニューをアプリにつけると楽ちんです。
次のコードをコピペして AppDelegate.swift
に置くだけで「UserDefaultsを消す」「Viewをリセットする」デバッグメニューができます。
// Debug View
extension UIWindow {
open override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
#if DEBUG
guard motion == .motionShake else {
return
}
let alert = UIAlertController(title: "デバッグメニュー", message: "デバッグメニューだよ!!", preferredStyle: .alert)
let clearCache = UIAlertAction(title: "UserDefaultsをクリア", style: .default, handler: { (_) in
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
})
let restartApp = UIAlertAction(title: "Viewをリセット", style: .default, handler: { (_) in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.rootViewController = storyboard.instantiateInitialViewController()
})
let cancel = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)
alert.addAction(clearCache)
alert.addAction(restartApp)
alert.addAction(cancel)
self.rootViewController!.present(alert, animated: true, completion: nil)
#endif
}
open override var canBecomeFirstResponder: Bool {
get {
return true
}
}
}