iPhoneの自動ロックを遅らせるが無効にしない
-
06-07-2019 - |
質問
現在、iPhoneを振るだけで操作できる非常にシンプルなアプリを持っています。ただし、iPhoneはタッチイベントを取得していないため、最終的に画面が暗くなり、自動ロックされます。振ったときに自動ロックタイムアウトをリセットする方法があるかどうか疑問に思っていましたか?
自動ロックを完全に無効にするには、次のようにします。
[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ]
しかし、私はそれを完全に無効にしたくありません。 iPhoneが合法的に使用されていない場合、期待どおりに自動ロックされるはずです。
ご協力ありがとうございます。
解決
独自のNSTimerまたは行動ジェスチャー(電話を振る)の値に基づいて、 [UIApplication sharedApplication] .idleTimerDisabled
の値を切り替えることができます。アプリケーションで複数回 YES
/ NO
に設定できます。
他のヒント
これは、アプリで使用するコードです。少し背景:アプリには組み込みのWebサーバーがあるため、ユーザーはWIFI経由でブラウザーからデータにアクセスでき、サーバーにリクエストが到着するたびに、ロックタイマーを延長します(この場合は最低2分間。再度有効にすると、デフォルトの時間が追加されます)。
// disable idle timer for a fixed amount of time.
- (void) extendIdleTimerTimeout
{
// cancel previous scheduled messages to turn idle timer back on
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(reenableIdleTimer)
object:nil];
// disable idle timer
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
// re-enable the timer on after specified delay.
[self performSelector:@selector(reenableIdleTimer) withObject:nil afterDelay: 60 * 2];
}
- (void) reenableIdleTimer
{
sharedApplication].idleTimerDisabled );
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(reenableIdleTimer)
object:nil];
// disable idle timer
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
所属していません StackOverflow