Display a relaodable view from AppDelegate based on Core Spotlight search term

751 Views Asked by At

I have Core Spotlight set up in my app. I navigate to a specific view controller within a navigation controller that displays a webview webpage based on the title. So for example:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray *restorableObjects))restorationHandler {

if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) {
    // This activity represents an item indexed using Core Spotlight, so restore the context related to the unique identifier.
    // The unique identifier of the Core Spotlight item is set in the activity’s userInfo for the key CSSearchableItemActivityIdentifier.

    NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier];
    NSString *valueForUID = [self valueForKey:uniqueIdentifier];

    self.tabBar = (UITabBarController *)self.window.rootViewController;
    self.tabBar.selectedIndex = 0;
    UINavigationController *selectedNav=(UINavigationController *)self.tabBar.selectedViewController;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WebViewController *webVCVC = [storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
    webVC.title = uniqueIdentifier;
    webVC.titleDetail = valueForUID;
    [selectedNav presentViewController:webVC animated:YES completion:nil];
}
}

In short, the webVC loads a specific web page based on it's self.title and titleDetail, these values change because every spotlight unique ID is different.

The issue isn't if the first spotlight index is selected, everything works fine. however, if I select the new 'Back To %@' button and go back to Spotlight to select a new search item, I get the following error:

Warning: Attempt to present <WebViewController: 0x7fced37c8be0> on <UINavigationController: 0x7fced403fa00> whose view is not in the window hierarchy!

So I thought all I had to do was dismiss the current view every time and reload it:

self.tabBar = (UITabBarController *)self.window.rootViewController;
self.tabBar.selectedIndex = 0;
UINavigationController *selectedNav=(UINavigationController *)self.tabBar.selectedViewController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
WebViewController *webVC = [storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
[webVC dismissViewControllerAnimated:NO completion:^{
   webVC.title = pubNum;
   webVC.titleString = pubTitle;
   [selectedNav presentViewController:webVC animated:YES completion:nil];
}];

This doesn't work either, I get the following warning/error:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

Which makes sense. How do i circumvent this in this particular instance?

2

There are 2 best solutions below

1
On BEST ANSWER

I fixed it by dismissing it from the navigation controller:

//Instead of this
[webVC dismissViewControllerAnimated...

//I used this
[selectedNav.presentedViewController dismissViewControllerAnimated...
1
On

A UINavigationController, even though it inherits from UIViewController, doesn't have a non-nil view of its own, so you can't call presentViewController:animated:completion: on top of it. I think that you're confusing presentViewController (which displays a modal, and needs to be sent to the view controller that's currently being displayed) with pushViewController:animated:, which is sent to the current view controller's navigationController.