美文网首页
8-11搜索相关

8-11搜索相关

作者: Zd_silent | 来源:发表于2016-08-11 10:21 被阅读36次

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

</br>
想在App中使用Spotlight,首先得引入Core Spotlight Framework,Targets ->General -> linked Frameworks and Libraries 点击加号添加CoreSpotlight.framework

导入头文件

#import <CoreSpotlight/CoreSpotlight.h>
#import <MobileCoreServices/MobileCoreServices.h>
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;
}

相关文章

  • 8-11搜索相关

    iOS搜索相关 项目用到search api 和 Spotlight以下是: search api : NS...

  • 搜索相关

    搜索技术 理解用户理解意图理解文档理解场景 query分析 检索纠错,同义归一,丢词 排序重要度,紧密度,同义词,...

  • 搜索相关技术

    1、爬虫 爬虫系统的设计要求:主流的搜索引擎爬虫能力超过30亿/每天对外界新数据有分钟级的感知能力对优质内容的覆盖...

  • 百度相关搜索是怎么出现的如何利用

    百度相关搜索形成原理 用户搜索词和其他搜索词出现相关的,或有很多人是直接搜索与这个词相关的其他短语,如果搜索的人多...

  • 站外推广第十二节:关键词挖掘之相关关键词

    一、相关词是什么? 相关词,其实就是搜索页面底部的相关搜索词,当我们在搜索框输入某些关键词之后,系统会根据我们搜索...

  • 爬取微博相关参数信息

    关于搜索参数1 综合2 主搜索页显示的实时3 用户5 相关图书6 相关音乐9 相关地点16 相关应用17 相关用户...

  • 搜索相关性得分

    介绍 本章将会介绍搜索的相关性得分,通过对不同算法的介绍来介绍es搜索背后的得分计算。 相关性 搜索的相关性算分,...

  • 如何寻找对标账号

    如何寻找对标账号? 一般都是直接去搜索同类的用户,搜索框下面有很多的选项,除了搜索用户还可以搜索相关的话题以及相关...

  • 实现百度右侧排名相关搜索全攻略

    早在几年前,搜索引擎界面就在搜索词界面的右侧增加了符合特定搜索条件的相关品牌,相关人物,相关企业等的推荐展示,这是...

  • ES 搜索相关整理

    控制台命令 PHP-SDK

网友评论

      本文标题:8-11搜索相关

      本文链接:https://www.haomeiwen.com/subject/yhrrsttx.html