LoginSignup
26
26

More than 5 years have passed since last update.

iOSで楽にデバッグメニューをつける

Last updated at Posted at 2017-07-02

アプリを開発していて、チュートリアルをたくさんやり直して動作確認をしたかったりする時がありますよね。
そんな時はデバッグメニューをアプリにつけると楽ちんです。

次のコードをコピペして 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
        }
    }
}
26
26
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
26
26