1
ユーザーが30秒間アクティブでない場合、新しいuiviewcontrollerをフォームシートとして表示するように、セッションの非アクティブを実装しています。タッチイベントのために、私はコンソールにタッチが目的のC言語で2回注目されている
#import "BCDTimeManager.h"
@implementation BCDTimeManager
__strong static BCDTimeManager *sharedTimerInstance = nil;
NSTimer *idleTimer;
NSTimeInterval timeinterval;
+ (BCDTimeManager*)sharedTimerInstance
{
static dispatch_once_t predicate = 0;
dispatch_once(&predicate, ^{
sharedTimerInstance = [[self alloc] init];
NSString *timeout = [[NSUserDefaults standardUserDefaults] valueForKey:@"session_timeout_preference"];
timeinterval = [timeout doubleValue];
});
return sharedTimerInstance;
}
- (void)resetIdleTimer {
if (idleTimer) {
[idleTimer invalidate];
}
idleTimer = nil;
NSLog(@"timeout is %ld",(long)timeinterval);
idleTimer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:true];
}
- (void)idleTimerExceeded {
NSLog(@"idle time exceeded");
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ApplicationTimeout" object:nil];
}
しかし、私は、画面上の任意のタッチ操作を行うとき、私が実装したシングルトンクラスresetIdleTimerとidleTimerExceed方法であるBCDTimeManagerクラスでこのコード
(void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// allTouches count only ever seems to be 1, so anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded) {
[[BCDTimeManager sharedTimerInstance]resetIdleTimer];
}
}
}
を使用しています私はNSLogが2回印刷され、NSNOtificationアクションが2回トリガーされることがわかります。
私は何が間違っているのか分かりません。これを理解するのを手伝ってください。
'' '' resetIdleTimer'''で '' 'idleTimer'''をどこで定義していますか? '' '[idleTimer invalidate]' 'を呼び出すと、存在しないようです。 – docksteaderluke
私はidleTimerをどこに定義しているかを示すコードを更新しました – Nitya
1つは、rootViewControllerにNSNotificationオブザーバを追加し、ユーザがログアウトしたときにオブザーバを削除することです。オブザーバが適切に削除されず、通知が2回追加される可能性があります。 – Nitya