Reputation: 104065
Modern mobile apps can receive truly asynchronous events coming from outside the app, like push notifications. Handling these events is a bit troublesome, because when the event arrives, the app may not be able to handle it right away. As an example, displaying a pop-up alert in reaction to a push notification may not be possible if there already is some pop-up displayed or if the UI is right in the middle of something else. How do you handle these events?
To be particular, the code for handling the asynchronous event can’t be simply placed in an ordinary UIViewController
, can it? There’s always a different controller on the screen depending on where the user is in the UI. While I could repeat the code in each view controller (or refactor it into a common ancestor), that feels like a hack.
If I handled the events in some background service object (which feels right), I still have to reach out to the “current active controller” and relay the event information. But there’s nothing like “current active controller” in iOS, and even if there was, the controller would still have to indicate whether it’s currently free to display the event or not. Again, it looks like there’s a ton of intricate corner cases to be handled.
In short, is there a pattern or library to handle such events without the logic spreading to the whole app?
I know this is sort of subjective, but I think there is a plain, constructive answer.
Upvotes: 0
Views: 162
Reputation: 1274
You can create a new UIWindow when the event appear and manage its key to display your dialog behind or above other dialog. In this way you don't have to take care about which UIViewController you are...
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = viewController;
window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
window.opaque = NO;
window.windowLevel = UIWindowLevelCFShareCircle;
window.backgroundColor = [UIColor clearColor];
[window makeKeyAndVisible];
Then you can manage your view in an other UIViewController. To remove the windows:
[window removeFromSuperview];
window = nil;
Hope that will help!
Upvotes: 1