Spotlight Search - deep link integration

887 Views Asked by At

I'm recieving results in spotlight but when the user taps on it, it only launches the app, I'm having trouble segueing to that specific product.

It would be much apprieated if someone can help me out!

Thanks!

Here is my code so far:

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

    if ([userActivity.activityType isEqualToString:Identifier]) {

        UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
        tabBarController.selectedIndex = 1;
    }

    return YES;
}
2

There are 2 best solutions below

0
On

Below checking may help you :

  1. Check whether the tabBarController assigned properly or is it nil?
  2. Is the UITabBarController delegate,didSelectViewController getting called ?
  3. Later on,Debug the each navigation section using breaking point and try to find out the issue
2
On

You are checking the activityType against the wrong value.

When launching an app from the spotlight, the activity type is CSSearchableItemActionType and the userInfo has a single key value pair CSSearchableItemActionType that contains your product identifier.

Simple Objective-C sample:

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    if ([userActivity.activityType isEqualToString:CSSearchableItemActionType])
    {
        NSString *uniqueIdentifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];
        // do stuff to show detail for product with uniqueIdentifier
        return YES; // because you did handle the activity
    }
    return NO; // because you did NOT handle the activity
}

Simple Swift sample:

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool
{

    if userActivity.activityType == CSSearchableItemActionType {
        let uniqueIdentifier = userActivity.userInfo[CSSearchableItemActivityIdentifier]
        // do stuff to show detail for product with uniqueIdentifier
        return true // because you did handle the activity
    }
    return false // because you did NOT handle the activity
}