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

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

More than 1 year has 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
        }
    }
}
Why do not you register as a user and use Qiita more conveniently?
  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
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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