开发者

Best way to handle suspended application when it has a session that should time out

开发者 https://www.devze.com 2023-01-27 06:21 出处:网络
I have an application which includes a login session. When the application is suspended (iOS 4), I save the current time and if the next time application becomes active is within 15 min from suspensio

I have an application which includes a login session. When the application is suspended (iOS 4), I save the current time and if the next time application becomes active is within 15 min from suspension time I want it to resume. Otherwise I want it to go back to login screen since the session is expired.

Here is what I am doing write now to implement it:

In app delegate's applicationDidBecomeActive, I check the time and present the login screen (modally) if needed. However the problem is that if the application was showing an UIAlertView or UIActionSheet when suspended, it will not dismiss it automatically when I present the login screen. The UIAlertView or UIActionSheet will then appear on the login screen which it does not belong to at all. I know I can register each UIAlertView and UIActionSheet to listen to applicationDidBecomeActive and dismiss if needed but since I have many of them all over my application it is really convenient if I can avoid that.

I was wondering if there is a way to dismiss all active views, which will stay on screen even if another view controller is presented modally.

Or if there is any better way to handle sessio开发者_如何学JAVAn time-outs in iOS 4?


I figured out a way to do this without Notifications (Although notifications are not as difficult as they seem to be): I added an iVar(s) to each class showing a UIAlertView to hold the currently displaying AlertView. In AlertView's definition:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: ....
[alert show];
self.alertView = alert;
[alert release];

And cleanup the iVar when AlertView is dismissed:

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)index {
    // code goes here
    self.alertView = nil;
}

Then in - (void) viewWillDisappear:(BOOL)animated or - (void)dealloc (depending on the situation) I added:

    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];

The same can be done for UIActionSheet. Hope this helps.

Please let me know if you have a better solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消