8-11搜索相关

iOS搜索相关 项目用到search api 和 Spotlight
以下是:
search api : NSUserActivity
在不依赖CoreSpotlight时用NSUserActivity实现搜索,必须要保证activity是强引用的,直到它被添加到索引。否则,您设置的 activity会在dealloc 的时候消失。
一:通过属性创建控制器的activity,保证activity是强引用的
二:是使用userActivity 的UIResponder属性,并重写updateUserActivityState方法

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
【8-11搜索相关】

想在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 = https://www.it610.com/article/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; }

    推荐阅读