问题
Sometimes when I open my app (either while resuming it after it was dormant for a while or while opening it after it has been 'quit'), the splash screen, which basically just says 'loading,' doesn't show. The app shows what seems to be the last shown view before the app was quit instead of the loading screen. This is bad because it makes it appear that the app has loaded when it has not and it seems like it is unresponsive even though it's just loading. Has anyone experienced anything like this before?
Edit: I am familiar with the iOS behavior which takes a picture on pause and uses it as a resume picture. Yet when the app has been quit (pressed home button, then hit home button twice, tap and hold app icon, then hit the red dash and quit it) and restarted, it doesn't use the splash screen on the next startup and still uses that last shown view. Can I force the app to always use the splash screen?
回答1:
The cause of the problem was that I was performing a long-ish operation upon applicationWillTerminate:, which delayed the 'true' termination of the app for a short bit (I assume). So, when I re-open the app right after closing it, this operation (writing data to disk) is still going on, so the app hasn't truly been quit. This is what I believe is the issue from what I can deduce.
回答2:
Are you certain that the app is really launching instead of just being suspended? The behavior your describe sounds exactly like that of an app that has been suspended and written out to secondary storage.
回答3:
Yes, that's working as intended. Every time your app is going to suspended state, iOS will take a screenshot and use it as a launching image on next launch, except if it's a fresh launch. So "or while opening it after it has been 'quit'" doesn't seem right. If that really happens, it means your app was not quit when you think it was.
Anyway, it doesn't matter if app launches from scratch or from background, you should always display your view as soon as possible, give user some kind of notice that it's loading, then handle loading content asynchronously, not have the user wait for unresponsive app hoping it will work some time soon.
回答4:
Like Caleb says, your application is being suspended but not really exiting. To force your app to exit when it's suspended, set UIApplicationExitsOnSuspend
to YES
in your ProductName-Info.plist.
回答5:
Device os create snapshot screen image when application goes in background, here is path you can find in sandbox of application "AppData/Library/Caches/Snapshots/".
Here is a trick that you can prevent to show last screen of app, when you launch application.
// Create subclass of UIImageView
@interface SnapShotImageView :UIImageView
@end
@implementation SnapShotImageView :UIImageView
@end
// Create function in appdelegate.m
- (void)createSnapshotWhileApplicationGoesInBackground{
NSString *splashImg = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
splashImg= @"Default~ipad.png";
}
else {
splashImg= @"Default~iphone.png";
}
UIWindow *win = [[UIApplication sharedApplication] keyWindow];
SnapShotImageView *splash = [[SnapShotImageView alloc] initWithImage:[UIImage imageNamed:splashImg]];
splash.frame = self.window.bounds;
[win addSubview:splash];
}
- (void)removeSnapshotFromWindow{
UIWindow *win = [[UIApplication sharedApplication] keyWindow];
if ([[win subviews] count] > 1) {
[NSThread sleepForTimeInterval:0.5];
NSArray *array = [win subviews];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[SnapShotImageView class]]) {
[obj removeFromSuperview];
}
}];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self removeSnapshotFromWindow];
}
//Call this function Application delegate method this way..
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self createSnapshotWhileApplicationGoesInBackground];
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
[self removeSnapshotFromWindow];
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
[self removeSnapshotFromWindow];
}
here we creating snapshot of splash screen.. just adding splash screen in window. when application goes in background
and removeing snapshot when application comes in foreground
来源:https://stackoverflow.com/questions/9552491/ios-app-shows-last-shown-view-instead-of-splash-screen-but-only-sometimes