My app has a somewhat complicated view stack. I am trying to allow the app to present content from Spotlight searches. The app handles two kinds of content. Video and PDF. All content metadata is stored in core data. I am using MMDrawerController to handle the presentation, not a split view, even though the result is similar.
My view progression:
Launch app...
- Present Sign In view. (Handle authentication)
- Present Data Loading View. (Check for new data, update)
- Present Home View. (MMDrawerController with a Table (menu) as left and a ViewController in center view (HomeView) ).
- When users select menu options, the center view changes to a content list.
- When a content item is selected, it sends the item to a coordinator which determines the content type, then loads the correct detail view.
I have implemented -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {...}
and am able to get back at the item when the user picks a spotlight result.
If the app is running in the background and currently on the Home Screen, I am able to load the appropriate detail view with no problem.
However, if the app is not on the home screen, Nothing seems to happen.
To keep the navigation stack clean, my plan was to first return the user to the home screen, then load the spotlight selected item. I am attempting to do this using notifications.
Here is my Notification setup:
- HomeView listens for spotlightItemSelected.
- ContentListView and both ContentDetailViews listen for goToHomeView.
When the app is not on the home view, it will either be on the ContentListView or one of the two ContentDetailViews.
As the Spotlight item is selected, the app successfully returns to the HomeView, but will not then present the newly selected item. It seems to have something to do with timing, but I am stuck...
Here is the complete method that is controlling this all...
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
[self performSelectorOnMainThread: @selector(postGoHomeNotification) withObject: nil waitUntilDone: YES];
if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {
//get UID
NSString *uniqueIdentifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];
// Handle 'uniqueIdentifier'
NSLog(@"uniqueIdentifier: %@", uniqueIdentifier);
//get contentItemID (nsstring) from uniqueIdentifier...
NSArray *components = [uniqueIdentifier componentsSeparatedByString:@"."];
NSString *cID = [components objectAtIndex:4];
//get content item with ID...
ContentItemFetcher *fetcher = [[ContentItemFetcher alloc] init];
ContentItem * fetchedItem = [fetcher fetchContentItemWithID:cID];
if (uniqueIdentifier != nil) {
[self performSelectorOnMainThread: @selector(postSpotlightSelectedNotification:) withObject: fetchedItem waitUntilDone: YES];
return YES;
}
}
return YES;
}
-(void) postGoHomeNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"goToHomeView" object:nil];
}
-(void) postSpotlightSelectedNotification:(ContentItem*) item {
[[NSNotificationCenter defaultCenter] postNotificationName:@"spotlightItemSelected" object: item userInfo:nil];
}
If I comment out the call to postSpotlightSelectedNotification, all views will successfully return to the HomeView.
If I comment out the call to postGoHomeNotification, the HomeView will process the postSpotlightSelectedNotification successfully.
With both of these in the method, I only get the return to the HomeView and not the loading of the selected item.
Any ideas?
Aucun commentaire:
Enregistrer un commentaire