I am trying to implement background refresh in my app. I have a method refreshMessages
that is inside one of my UIViewControllers
(I am using a UINavigationController
). I need to access this controller from AppDelegate.m
.
Usually I would do something like this:
UINavigationController *navigationController = [self.window.rootViewController navigationController];
to get the navigation controller and I can then access them from there. But surely this won't work now. The app is in the BACKGROUND and no window is showing. Any thoughts on how to get around this? Thanks!
Here is my background refresh:
-(void)application:(UIApplication *)application
performFetchWithCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"Doing the background refresh");
UINavigationController *navigationController = [self.window.rootViewController navigationController];
NSLog(@"Number of controllers is %d", [[navigationController viewControllers] count]);
//There are zero controllers?!!! Must be because no window is showing
completionHandler(UIBackgroundFetchResultNewData);
}
Yes, you are right. The
window
will benil
since it will no longer be in the view hierarchy.A cheeky solution would be to hold the reference of the view controller in the
AppDelegate
by creating a property in it. You can assign this when the application goes to the background by subscribing to the relevant notification in the view controller.Edit: Code added
In your
AppDelegate.h
In YourViewController.m, somewhere in
viewDidLoad
appWillResignActive
's implementationIn your completion handler
Also, make sure you remove the observer once the view controller is deallocated.