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.
精彩评论