当我们回到我们的应用程序

时间:2015-10-13 10:58:41

标签: ios iphone ipad xcode7 corespotlight

大家好我正在使用名为spotlight功能的新iOS 9功能。我无法获得我在聚光灯搜索中写的文字,当我们从聚光灯搜索回到我的应用程序时我正在使用corespotlight框架

- (void)setUpCoreSpotLightSearch {

    self.userActivity = [[NSUserActivity alloc]
                         initWithActivityType:@"com.pulpstrategy.spotlight"];
    self.userActivity.title = @"Test";

    self.userActivity.userInfo = @{@"subjectid" : @"5" , @"subjecttype" : @"type"};
    NSLog(@"userInfo %@", self.userActivity.userInfo);

    self.userActivity.eligibleForSearch = YES;
    self.userActivity.eligibleForPublicIndexing = YES;
    self.userActivity.webpageURL = [NSURL URLWithString:@"http://www.google.com"];

    CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:(NSString *)kUTTypeImage];
    attributeSet.title = @"My";
    attributeSet.contentDescription = @"";
    attributeSet.keywords = [NSArray arrayWithObjects:@"Hello",@"Welcome",@"Utkarsh", nil];
    UIImage *img = [UIImage imageNamed:@"A.jpeg"];
    NSData *imgData = [NSData dataWithData:UIImagePNGRepresentation(img)];
    attributeSet.thumbnailData = imgData;

    CSSearchableItem *item = [[CSSearchableItem alloc]initWithUniqueIdentifier:@"com.pulpstrategy.spotlight" domainIdentifier:@"spotlight.CoreSpotLight" attributeSet:attributeSet];

    [[CSSearchableIndex defaultSearchableIndex]indexSearchableItems:[NSArray arrayWithObject:item] completionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Item = %@",error.localizedDescription);
        }
    }];

}

在搜索之后我正在使用:

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

    userActivity.eligibleForSearch = YES;

    NSLog(@"Handler = %@",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.userActivity valueForKey:uniqueIdentifier];// The app is crashed that line piece of code.

        ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

       NSLog(@"value for UID = %@",valueForUID);

        [navController pushViewController:viewController animated:YES];

    }
    return YES;
}

0 个答案:

没有答案