Save UITableView property when applicationDidEnterBackground and load back it applicationDidBecomeActive in iOS - ios

In my iOS application, I'm having a UITableView inside my UIViewController. After data loading completed to the UITableView, when I press the Home button of the iPhone, the application will enter to the background. It will execute following method.
- (void)applicationDidEnterBackground:(UIApplication *)application {
When I tap on the application icon and launch the app, it will call following methods in AppDelegate
- (void)applicationWillEnterForeground:(UIApplication *)application {
and
- (void)applicationDidBecomeActive:(UIApplication *)application {
but none UIViewController methods. Therefore what I did was, called a custom method created by myself which is located inside my UIViewController class like below. This code go inside applicationDidBecomeActive method.
MyViewController *tViewCont = [MyViewController alloc];
[tViewCont remindToPopulate];
I put a log message and confirmed that remindToPopulate method is executing. Inside that method I want to reload the UITableView.
But at this time the UITableView property that I've declared is set to nil. What is the proper way of saving that UITableView property and load it back with the latest data?

For that you can add notification for UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification in viewDidLoad in controller.
just add
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(yourmethod) name:UIApplicationDidBecomeActiveNotification object:nil];
and in yourmethod
-(void)yourmethod{
[yourtableview reloadData];
}
you can also add UIApplicationWillEnterForegroundNotification notification. add as per your need.

Related

performSegueWithIdentifier before viewDidLoad and viewDidAppear sometimes it works and sometimes not

When I call performSegueWithIdentifier before viewDidLoad and viewDidAppear sometimes it works and sometimes not.
AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"applicationDidBecomeActive");
[self abcNotif]; // the method post the notification.
}
VC1:
-(void)awakeFromNib {
NSLog(#"awakeFromNib");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(theNotif:) name:#"abcNotif" object:nil];
}
-(void)theNotif:(UILocalNotification*)notif {
if([[[NSUserDefaults standardUserDefaults]valueForKey:#"flag"]isEqualToString:#"YES"]) {
[self performSegueWithIdentifier:#"seg1" sender:self];
NSLog(#"theNotif = %#", [[notif userInfo]valueForKey:#"notif1Key"]);
}
}
Logs in console
awakeFromNib
didFinishLaunchingWithOptions
applicationDidBecomeActive
theNotif = notif1Value
viewDidLoad
All is working fine although performSegueWithIdentifier is called before before viewDidLoad and viewDidAppear. But in some scenarios this don't work. Why is this behavior. Peoples have also asked theses kind of questions Why doesn't performSegueWithIdentifier work inside viewDidLoad?
The key thing to understand is that UIViewController subclasses load (create) their view property lazily. So viewDidLoad will be called after the view is loaded, but when is that? Certainly the normal cycle of launching your app causes it at some point, adding it to the window, but you can cause it to happen earlier by making a call on the view. Try inserting [self view]; immediately before your call to performSegue.. , this will ensure the view is loaded. Even so, it does not ensure that the view has been added to the window, and if that hasn't yet happened then I don't see how a modal can be pushed.

applicationDidEnterBackground not running

I am putting this in my ViewController.m file and when my app enters the background the NSLog is never called.
Can anyone explain why?
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"Application entered background state.");
}
This is app's delegate method. Put it in the object implementing the UIApplicationDelegate protocol which is by default app's delegate class created for you when you start a new project.
Or use the notification center to get notified about an event of switching to the background. Just register your view controller as an observer of UIApplicationDidEnterBackgroundNotification. It's sent when entering the background.
Example
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

iOS: Refresh Data When App is Brought to Foreground

I'm getting data off the server via JSON and displaying it on Labels.
I've added that method in viewDidLoad.
I want to refresh the data when the user opens the app again. Currently, even if I kill the app in the simulator and start the app again, it doesn't refresh.
I tried the viewDidAppear method, but it isn't being executed for some reason.
-(void)viewDidAppear{
NSLog(#"Called viewDidAppear");
}
This is never called. I tried to minimize the app but it didn't work.
You can listen for notifications and respond appropriately. Try using these and decide what works for your intended workflow.
UIApplicationDidBecomeActiveNotification
UIApplicationWillEnterForegroundNotification
You can use respond to the notification like this.
[[NSNotificationCenter defaultCenter] addObserverForName: UIApplicationDidBecomeActiveNotification object: nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
// LOAD JSON
}];
I followed this tutorial - http://leejon.es/notifying-a-viewcontroller-with-uiapplicationdidbecomeactivenotification/
First, attach to the notification in the viewWillAppear method of the target view controller:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector( appActivated: )
name: UIApplicationDidBecomeActiveNotification
object: nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self ];
}
- (void)appActivated:(NSNotification *)note
{
[self update];
}
The viewDidAppear: method takes a bool parameter wether the view was displayed with an animation which you are missing. Also you have to call the implementation of the superclass:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear: animated];
NSLog(#"Called viewDidAppear");
}
In your app delegate implementation, there is a method called:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
This method is called each time the app is launched, so I think it fits your needs. If you place your code here, it should work.
Also, be aware you should not perform a synchronous call here, because you will delay the app launch.
EDIT:
This method will be only called when the app launches. You could place your code inside a method, and call it from application didFinishLaunchingWithOptions, and then also call it from the method:
- (void)applicationWillEnterForeground:(UIApplication *)application;
This method will be called when the application enters the foreground, but not after the first launch, so beware.
I also think you should check the UIApplicationDelegate methods from apple developer page: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Also, check out the application state changes:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Call a method every time a UIViewController is shown

I want to show a full page image Ad every time a UIViewController is shown.
I think I have to call the method inside a viewDidAppear or ViewWillAppear, but they are being called once.
- (void) viewDidAppear:(BOOL)animated{
[self showAds];
}
- (void) showAds{
//Do Something
}
What should I do to call a method every time a uiviewcontroller is shown( even if its already created)?
ViewWillAppear will be called every time a UIViewController is shown,but won't be called when the app is back to foreground.
you can use Notification to achieve your goal by following code,
This scenario is specially when your app is in background and user press HOME button to active it.
Register for Notifcation when your application enterForground in viewDidLoad only.
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleEnteredBackground)
name: UIApplicationDidBecomeActiveNotification
object: nil];
write a method to invoke when application enterForground
-(void)handleEnteredBackground
{
NSLog(#"%s",__FUNCTION__);
// Your stuff here
}
Dont forget to Remove Observer in viewDidUnload method
[[NSNotificationCenter defaultCenter] removeObserver:self];
Post New Notification everytime your application enterForground
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidBecomeActiveNotification object:nil];
}
ViewWillAppear should be called every time. Use:
- (void) viewWillAppear:(BOOL)animated{
[self showAds];
}

Which delegate function is called within a class on tapping the home-button

I have a pop-up which has to be hidden when the user moves away from the class.
On tapping on the home button, the doesn't happen.
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
Other than the above functions is there any other delegate functions which would be called in the same class (not the app-delegate class).
Only the UIApplicationDelegate defines those methods. If you want any other class to handle those events, you need to have the class register for the corresponding notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
And don't forget to remove the observer.
Then you need the method:
- (void)backgrounding {
// App entered background
}

Resources