Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

次のコードをコピペして 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
        }
    }
}
kaiinui
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away