2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Xcode12】「UIApplication.shared.isIdleTimerDisabled = false」を書いたのにiPhone実機がスリープしない【Swift5】

Posted at

はじめに

アプリがスリープモードにならない状態とスリープモードになる状態をコードで書いて試していました。
なぜか、スリープ状態になるコードを書いたのにスリープにならないという状態になりました。
そこで今回調べたので記事を書きました。

環境

Xcode12.3
Swift5.3.2
macOS Catalina 10.15.4

実行した事

 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        UIApplication.shared.isIdleTimerDisabled = false
}

※ UIApplication.shared.isIdleTimerDisabled = true にしてスリープモードを解除し、再度スリープモードにするという事を試していました。

結果

アプリがスリープモードになりませんでした。

原因

参照サイトに書かれているように、Xcode側からiPhone実機をビルドしている事が原因でした。
Xcodeと実機の接続を切って再度実行したところ、スリープになりました。

補足

公式よりdefaultでは「 UIApplication.shared.isIdleTimerDisabled = false」に設定されています。

//スリープモードを無効
 UIApplication.shared.isIdleTimerDisabled = true
//スリープモードを有効
 UIApplication.shared.isIdleTimerDisabled = false

参照サイト

https://iritec.jp/web_service/3740/
https://developer.apple.com/documentation/uikit/uiapplication/1623070-isidletimerdisabled
https://qiita.com/imj164/items/f35187a8fe84d9a0cd35

終わりに

Xcode側からiPhone実機をビルドする事も、Xcode側からiPhone実機の接続を切って実行する事も同じ事だと思っていたので勉強になりました。

Twitterやっています。

2
2
4

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?