I have a tabbed application with 2 storyboards. In the 2nd storyboard I have a restoration-ID.
I implemented the following in the AppDelegate:
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
and this in my SecondViewController:
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.myTextView.text forKey:#"unsavedText"];
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
self.myTextView.text=[coder decodeObjectForKey:#"unsavedText"];
[super decodeRestorableStateWithCoder:coder];
}
But when i go to Home with iOS simulator , stop the running application and restart, the TextView doesn't restore text, my application starts in a first tab.
You may have missed assigning a restoration ID elsewhere in your view hierarchy.
Did you also change your AppDelegate from -application:didFinishLaunchingWithOptions: to application:willFinishLaunchingWithOptions:?
There are some useful state restoration tools available at the Downloads for Apple Developers page.
Search for restoration to find a debug profile which logs your app's state restoration to the console, as well as the restorationArchiveTool to dump the state restoration data in human-readable format.
Related
I am facing the issue in preserving the state of my application.
My application is based on storyboard, this is how it goes.
First I have a navigation controller, then a login screen. When user logs in, I push it to a controller, which have table view on left side, with UITabbar Items, this is mainly a custom tab bar. On clicking every button, I add that container view to the the self. The application state preserves till here, But whenever I push further, it doesn't preserve and restore the state. On the custom tab bar, I have even encode and decoded the child view controller, and add accordingly when state restores. But when I navigate further it doesn't. Further I saw a strange behavior, If I initially hit the first tab item and move further in the navigation, it preserve and restore the state further.
One more thing, when I run the application on the simulator by hitting stop and play button, it shows the preserve view for few seconds and come back to the custom tab bar.
Any help would be appreciated.
My Tabbar code is
// encodeRestorableStateWithCoder is called when the app is suspended to the background
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(#"ParentViewController: encodeRestorableStateWithCoder");
[coder encodeObject:theNavigationController forKey:#"RootNav"];
[coder encodeObject:ssTodayNaVController forKey:#"TodaysCallNavEncode"];
// remember our children view controllers
[coder encodeObject:self.ssDasboardVC forKey:#"DashboardChild"];
[coder encodeObject:self.ssMessageVC forKey:#"MessageChild"];
[coder encodeObject:self.ssSearchCustomerVC forKey:SEARCH_CUSTOMER_SCENE];
[coder encodeObject:self.ssTodaysCallVC forKey:TODAYS_SCENE];
// remember the segmented control state
[coder encodeInteger:self.tabBar.mSelectedIndex forKey:#"selectedIndex"];
[super encodeRestorableStateWithCoder:coder];
}
// decodeRestorableStateWithCoder is called when the app is re-launched
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(#"ParentViewController: decodeRestorableStateWithCoder");
theNavigationController=[coder decodeObjectForKey:#"RootNav"];
ssTodayNaVController=[coder decodeObjectForKey:#"TodaysCallNavEncode"];
self.tabBar.mSelectedIndex = [coder decodeIntegerForKey:#"selectedIndex"];
[[NSNotificationCenter defaultCenter]postNotificationName:TABBUTTON_NOTIFICATION object:[NSNumber numberWithInteger:self.tabBar.mSelectedIndex]];
[self.tabBar reloadData];
[super decodeRestorableStateWithCoder:coder];
}
And if I print the saved controllers with this method, it prints but doesn't save
- (UIViewController *)application:(UIApplication *)application
viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents
coder:(NSCoder *)coder {
// return various navigation controllers here.
// actual view controlers will each be returned in their own classes
SCLogNotice(#"Saved Controllers are %#",identifierComponents);
return nil;
}
Cheers
In my application, I need to make a call, So obviously my app go to background while making a call using native call feature.if the call goes long my app is getting killed by IOS itself. Now i need to restore the last view at the time of making a call. I have used Native restoration. What i did is
1. Set the restoration ID for all the views and view controllers.
2. Override the app delegate restoration methods.
My Issue is,
If my app go to background and come back to foreground, Last view is displayed using preservation and suddenly moved to main view(Default launch view). just like last view blinking while coming to fore ground.
Here is my setting:
app Delegate code :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Main view settings :
Main storyboard contains the login view as a root. Please guide me to fix the restoration issue.
I managed to get rid of the blinking by making the window key and visible in application:willFinishLaunchingWithOptions:.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
return YES;
}
I've seen many conflicting answers that seem to be situation dependent, but I haven't figured out one that works for me. I'm building a tabbed application in Storyboard, and I want to save the state of a view controller inside one tab that lives inside a navigation controller. It has dynamically created objects (labels and pictures) that I want to preserve when the app is shut down.
Thus far, I have the following in AppDelegate.m:
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
return YES; }
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
return YES; }
and in my viewController.m file:
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[coder encodeObject:self.view forKey:#"view"];
[super encodeRestorableStateWithCoder:coder]; }
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder {
self.view = [coder decodeObjectForKey:#"view"];
[super decodeRestorableStateWithCoder:coder]; }
I've also assigned RestorationID's to UITabBarController, the navigation controller, and the view controller in question.
Right now, if I minimize and restore, data is preserved, but if I stop and restart using xcode, it's lost. What am I missing?
Try to run an app, push the Home button twice to go to the app switcher, swipe your app away to close it and run it by clicking the icon on home screen (not from Xcode!). Is now everything ok?
I suppose when you run your app from Xcode it not just relaunches it, but reinstalls it, so that is not exactly what you need.
I am new to Objective-C and I'm making my first app, a single-view app. When I run my program ,the fullscreen ad that is supposed to appear (I'm using RevMob) appears. However, when I exit the ad I get a black screen with a blue status bar at the top.
I have tried many things, such as setting my main view controller as initial view controller, restarting my computer, changing/removing debugger, resetting the iOS simulator, etc.
My Xcode version is 4.6 and my OS is mac OSX 10.8.4
I don't want to delete Xcode and I also don't want to remove ads because that is my only source of income.
Here is my code:
Appdelegate.m
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[RevMobAds startSessionWithAppID:#"myappid"];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
RevMobFullscreen *ad = [[RevMobAds session] fullscreen];
[ad loadWithSuccessHandler:^(RevMobFullscreen *fs) {
[fs showAd];
NSLog(#"Ad loaded");
} andLoadFailHandler:^(RevMobFullscreen *fs, NSError *error) {
NSLog(#"Ad error: %#",error);
} onClickHandler:^{
NSLog(#"Ad clicked");
} onCloseHandler:^{
NSLog(#"Ad closed");
}];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
#end
Appdelegate.h:
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic)UIWindow *window;
#end
There is nothing wrong with the ad code. What seems to be (not) happening is that your app has no content. I suspect that you haven't set up the rootViewController (initial view controller) for the app correctly.
This is what you need to do:
Get rid of all of the app ad code and just make an app that works. All it has to do is correctly show an initial view controller. You could just make a new project in XCode using the Single View Application template.
Add your app code as per this example. You will invoke the code from your initial view controller via a button.
Once that is working, you can add the code into - (void)applicationDidBecomeActive:(UIApplication *)application as per your question. When you dismiss the advert, you should see your initial view controller.
I read in the documentation that the #property(nonatomic, copy) NSString *restorationIdentifier is capable of preserving the state of an UIImageView properties such as position, angle, etc. I tried adding the methods
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
to the view controller. I have set the restoration ID of the view controller as #"myFirstViewController in the IB.
I have added the following methods to the view controller as well.
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:_myImageView.image forKey:#"UnsavedImage"];
[super decodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
_myImageView.image = [coder decodeObjectForKey:#"UnsavedImage"];
[super encodeRestorableStateWithCoder:coder];
}
Am i supposed to add the first two methods in the appDelegate or the view controller?
The UIImageView is not getting preserved. What is wrong here?
To make the state preservation and restoration work there are two steps that are always required:
The App delegate must opt-in
Each view controller or view to be
preserved/restored must have a restoration identifier assigned.
You should also implement encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: for views and view controllers that require state to be saved and restored.
Add the following methods to the view controller of your UIImageView.
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:UIImagePNGRepresentation(_imageView.image)
forKey:#"YourImageKey"];
[super decodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
_imageView.image = [UIImage imageWithData:[coder decodeObjectForKey:#"YourImageKey"]];
[super encodeRestorableStateWithCoder:coder];
}
State preservation and restoration is an optional feature so you need to have the application delegate opt-in by implementing two methods:
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
Useful article about state preservation:
http://useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html