首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有corespotlight搜索功能的示例代码- iOS 9API?

是否有corespotlight搜索功能的示例代码- iOS 9API?
EN

Stack Overflow用户
提问于 2015-06-26 08:37:48
回答 6查看 15K关注 0票数 24

是否有corespotlight搜索功能的示例代码- iOS 9API?如果能看一下实现/测试的示例代码,我会很感激。

EN

回答 6

Stack Overflow用户

发布于 2015-07-10 22:37:15

  1. 创建一个新的iOS项目,并将CoreSpotlight和MobileCoreServices框架添加到项目中。

  1. 创建实际的CSSearchableItem,并将uniqueIdentifier、domainIdentifier和attributeSet关联起来。最后,使用[CSSearchableIndex defaultSearchableIndex...]为CSSearchableItem建立索引如下所示。

  1. OK!测试索引!

票数 39
EN

Stack Overflow用户

发布于 2015-06-26 09:27:39

代码语言:javascript
复制
CSSearchableItemAttributeSet *attributeSet;
attributeSet = [[CSSearchableItemAttributeSet alloc]
                                 initWithItemContentType:(NSString *)kUTTypeImage];

attributeSet.title = @"My First Spotlight Search";
attributeSet.contentDescription = @"This is my first spotlight Search";

attributeSet.keywords = @[@"Hello", @"Welcome",@"Spotlight"];

UIImage *image = [UIImage imageNamed:@"searchIcon.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;

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

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item]
                                 completionHandler: ^(NSError * __nullable error) {
    if (!error)
        NSLog(@"Search item indexed");
}];

注意:kUTTypeImage要求您导入MobileCoreServices框架。

票数 13
EN

Stack Overflow用户

发布于 2015-10-20 19:48:44

为了完成spotlight搜索功能,一旦你实现了 answer,你将能够在搜索中看到结果,但在选择结果时,你的应用程序将简单地打开相关视图,而不是相关内容。

为此,请转到AppDelegate.m并添加以下方法。

代码语言:javascript
复制
 -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler

        {

            //check if your activity has type search action(i.e. coming from spotlight search)
            if ([userActivity.activityType isEqualToString:CSSearchableItemActionType ] == YES) {

                //the identifier you'll use to open specific views and the content in those views.
                NSString * identifierPath = [NSString stringWithFormat:@"%@",[userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier]];

                if (identifierPath != nil) {

                    // go to YOUR VIEWCONTROLLER
                    // use notifications or whatever you want to do so

                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
                    MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

                    // this notification must be registered in MyViewController
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"OpenMyViewController" object: myViewController userInfo:nil];


                    return YES;
                }

            }


            return NO;
        }

确保在AppDelegate.m中导入

代码语言:javascript
复制
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <CoreSpotlight/CoreSpotlight.h>

Swift 2.1更新

代码语言:javascript
复制
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {

    if #available(iOS 9.0, *) {
        if userActivity.activityType == CSSearchableItemActionType  {

            //the identifier you'll use to open specific views and the content in those views.
            let dict = userActivity.userInfo! as NSDictionary
            let identifierPath  = dict.objectForKey(CSSearchableItemActivityIdentifier) as! String
            if identifierPath.characters.count > 0 {

                let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let mvc: MyViewController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! MyViewController

                NSNotificationCenter.defaultCenter().postNotificationName("OpenMyViewController", object: mvc, userInfo: nil)
            }

            return true
        }

    } else {
        // Fallback on earlier versions
            return false

    }

    return false

}

确保在AppDelegate.swift导入:

代码语言:javascript
复制
import CoreSpotlight
import MobileCoreServices
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31063070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档