I want to show a Splash screen when app remain in background for more than 3 minutes. I started Background timer to trigger the function that will handle the UI update.
- (void)applicationDidEnterBackground:(UIApplication *)
if (application.applicationState == UIApplicationStateBackground) {
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
//and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
splashTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:splashTimer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
Now when the timer is invoked i call the function to update my UI i.e adding an image in the window so make it look like that it is a Splash Screen.
- (void)timerFired
{
[splashTimer invalidate];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIImageView *splash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Splash_Screen.png"]];
splash.frame = self.window.bounds;
[self.window addSubview:splash];
}];
}
and then remove This image from the window when Application Enter foreground again
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ([[self.window subviews] count] > 1) {
[NSThread sleepForTimeInterval:1.0];
[[[self.window subviews] lastObject] removeFromSuperview];
}
}
but my Splash Screen is not shown when i call it from a background thread. I tried everything but no luck. My Problem is that My Timer is being invoked from a Background thread and if i want to add image from this thread the UI is not updated. Any Help?
Springboard does not update your app's snapshot[1] when all background tasks have expire/ended. IIRC when your application is in the background, the snapshot is only updated just before your app is suspended after handling these background events:
[1] The image that appears in the task switcher and while your application is being moved back to the foreground.