Changing the UIView when starting the WillEnterForeground application

I am looking for a way to capture the current view displayed to the user when they send the application in the background.

The situation is that I have provided options in my set of parameters for the look and style that I would like to take effect when I run applicationWillEnterForeground.

Anyone have a suggestion on how to attack this issue?

I tried to register NSNotification, but it starts late, and the view appears to the user before running the NSNotification method.

+7
source share
4 answers

To capture the current view displayed when the user uses your application, use the AppDelegate applicationDidEnterBackground method.

 - (void)applicationDidEnterBackground:(UIApplication *)application { // Get current view... // (get from UITabbarController or whatever logic you like) // For this code sample, just grab first UIView under the main window UIView *myView = [self.window.subviews objectAtIndex:0]; } 

To change the view before displaying it, use the AppDelegate applicationWillEnterForeground method as follows:

 - (void)applicationWillEnterForeground:(UIApplication *)application { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 80, 80)]; label.backgroundColor = [UIColor whiteColor]; label.text = @"yo"; [self.myLastView addSubview:label]; } 

I understand that you mentioned applicationWillEnterForeground in your question. Can you clarify what you are trying to do, or possibly include code? I really tried the code above and the view update (I see the "yo" label above my view) .

My last suggestion is to try [view setNeedsDisplay] and [view setNeedsLayout] , which should make UIView redraw itself. (note - the problem is not that uiview changes are not displayed, the problem is that they are displayed after the old view is displayed. It seems that the picture was taken before the application was paused, and this screenshot is initially displayed when the application resumes for performance)

Edit

The code above is a good example of what the user sees. The label "yo" appears, but only after restoring the view. I will see if I can fix this and send a message.

Edit 2

It seems like resuming a paused application will show a captured screenshot of the last known application state, as far as I can tell. I tried to find documents on this subject, but I can not find it.

Alternative Solution 1

Remove the application when the user clicks the home button.

If you save the state of your application in applicationWillResignActive: or applicationWillTerminate: you can simply restore this state (first go to the correct user tab), and in appDidFinishLaunchingWithOptions: you can update the user interface before the user sees this. To close the application when it was inserted (paused), enter the key "The application does not work in the background" - the raw key: UIApplicationExitsOnSuspend before YES .

Doing this ensures that the user sees your application the way he configured it in the settings, without seeing the flickering of what he looked like. I wish I could find a better way to do this. :(

Alternative Solution 2

Show your settings inside your application.

This will obviously require a design change, however, more and more applications display settings within the application. Popular third-party library In the application parameter set . This allows you to configure parameters through the bundle / plist file in the same way as for global application settings. It also looks and behaves the same as global settings. This will give you FULL control of any behavior you want in your application.

+7
source

I'm going to guess that you have it back.

The system can take a screenshot of the current screen, and it shows this when your application is brought back to the fore. Perhaps this snapshot shows an old look before you can change it to applicationWillEnterForeground: time.

So, change when you get applicationWillEnterForeground :, then the snapshot will be taken from what you want to restore.

0
source

In each viewDidLoad you can always send a notification, see NSNotification Class Reference , you can either save the link to the view in NSUserDefaults , then do what you need to do when the user returns to applicationWillEnterForeground:

0
source

Just in case, someone comes up with the same problem:
It seems that Apple has implemented a new property in iOS7 on UIApplication that prevents the use of a previously taken snapshot when re-entering the foreground - ignoreSnapshotOnNextApplicationLaunch

0
source

All Articles