8-11検索関連

4675 ワード

iOS検索関連


プロジェクトで使用されるsearch apiとSpotlightは次のとおりです.

search api : NSUserActivity


CoreSpotlightに依存しないでNSUserActivityで検索を実行するには、インデックスに追加されるまでactivityが強く参照されることを保証する必要があります.そうでなければ、設定したactivityはdealloc時に消えます.
一:属性によってコントローラのactivityを作成し、activityが強い参照であることを保証する
二:userActivityのUIresponderプロパティを使用し、updateUserActivity Stateメソッドを書き換える
1. 
@property (nonatomic, strong) NSUserActivity *userActivity;
2. 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
    {
        // , type 
        self.userActivity = [[NSUserActivity alloc] initWithActivityType:@"SearchTest"];
        // 
        self.userActivity.title = @" - 、 ";
        //  
        self.userActivity.keywords = [NSSet setWithArray: @[@" ",@" "]];
        //  Search
        self.userActivity.eligibleForSearch = YES;
        // 
        [self.userActivity becomeCurrent];
    }

3. app
- (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{
    
    NSString *activityType = userActivity.activityType;
    if ([activityType isEqual:LU9_UserActivitySearch])
    {
        return YES;
    }
    return NO;
}

Spotlight


AppでSpotlightを使用するには、まずCore Spotlight Frameworkを導入し、Targets ->General -> linked Frameworks and Librariesプラス記号をクリックしてCoreSpotlight.frameworkを追加しなければなりません.

ヘッダファイルのインポート

#import 
#import 
1. 
- (void)supportSpotlightSearch {
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
      dispatch_async(queue, ^{
          @try {

                // SearchableItems 
                NSMutableArray *searchableItems = [[NSMutableArray alloc] initWithCapacity: ];
                for (  *akind in array) {
                    //1. 
                    CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*) kUTTypeImage];
                    
                    //2. 
                    attributeSet.title = akind.fTitle;
                    attributeSet.contentDescription = [NSString stringWithFormat:@"%@",  akind.content];
                    
                    //                attributeSet.thumbnailData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i+1]]);
                    //
                    //3. 
                    CSSearchableItem *searchableItem = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%@", akind.fid] domainIdentifier:@"SpotLight_Category" attributeSet:attributeSet];
                    
                    // 
                    [searchableItems addObject:searchableItem];
                }
             //4. 
             [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
                 if (!error) {
                     NSLog(@"%s, %@", __FUNCTION__, [error localizedDescription]);
                 }
             }];
         }
         @catch (NSException *exception) {
             NSLog(@"%s, %@", __FUNCTION__, exception);
         }
         @finally {
        }
     });
 }
2. app
- (BOOL)application:(nonnull UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler{
        NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:@"kCSSearchableItemActivityIdentifier"];
         //     ~~~,      
        // ,   
        [kAppDelegate.centerViewDeckController closeOpenView];
        for (UINavigationController *na in kAppDelegate.tabBarController.viewControllers)
        {
            [na popToRootViewControllerAnimated:NO];
        }
        
        [kAppDelegate.tabBarController setSelectedWithIndex:0];
        
        
        Kind *kind = [[Kind alloc] init];
        kind.fid    = uniqueIdentifier;
        kind.fTitle = userActivity.title;
        
        CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
        categoryVC.kind = kind;
        [kAppDelegate.homeNavigationController pushViewController:categoryVC animated:YES];
        
        return YES;
}