App screen snapshot being shown instead of launchScreen during state restoration

前端 未结 1 1712
迷失自我
迷失自我 2021-01-15 01:29

I encountered this situation where I had added app state restoration APIs for the first time to a new app supporting iOS 9.3 and higher. The state restoration is working f

相关标签:
1条回答
  • 2021-01-15 02:24

    After researching this I found that Apple has long ago provided a method off of UIApplication to deal with this situation. But its usage, even today, is poorly documented.

    The solution is to use the ignoreSnapshotOnNextAppliationLaunch method from UIApplication.

    Apple ignoreSnapshotOnNextApplicationLaunch method

    You will have to access it via the UIApplication singleton pattern as suggested by Apple here as I will explain:

    Apple UIApplication sharedApplication method

    The where to use this is what is not clearly documented and that I am sharing here. The ignoreSnapshotOnNextApplicationLaunch method will have absolutely no effect unless specifically called when iOS is saving the app state from the view controller(s). Such as when you tap the home button to background the app.

    You cannot call this method directly from the AppDelegate methods dealing with background / foreground transitions, as it needs to be called from the view controllers while their states are being saved for later restoration.

    For this saving task Apple provides the encodeRestorableStateWithCoder method from UIViewController

    Apple encodeRestorableStateWithCoder method

    And this is where we need to make the change. If doing state restoration you should already have it; but by adding this method call to each view controller class where you have setup restoration IDs in storyboard, or are saving state manually, you can avoid any snapshots being used by including the ignoreSnapshotOnNextApplicationLaunch from the UIApplication singleton. This will not prevent iOS from taking the snapshot, just not showing it during app state restoration on re-launch.

    // save any app state information that is not already saved automatically
    - (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
    
        // prevent taking a screen shapshot and force launchScreen xib to be used always
        [[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
    
        [super encodeRestorableStateWithCoder:coder];
    
        return;
    }
    

    Be sure you re-background the app after adding this during your testing, to have iOS delete the previously saved snapshot file.

    0 讨论(0)
提交回复
热议问题