是否有针对corepotlight搜索功能的示例代码 - iOS 9 API?

moj*_*jo_ 23 objective-c ios9 xcode7 corespotlight

是否有针对corepotlight搜索功能的示例代码 - iOS 9 API?真的很感激,如果可以查看示例代码来实现/测试.

may*_*yue 37

  1. 创建一个新的iOS项目,并将CoreSpotlightMobileCoreServices框架添加到您的项目中. 在此输入图像描述

  2. 创建实际的CSSearchableItem并关联uniqueIdentifier,domainIdentifier和attributeSet.最后使用[[CSSearchableIndex defaultSearchableIndex] ...]索引CSSearchableItem,如下所示. 在此输入图像描述

  3. 好的!测试索引!
    在此输入图像描述

  • 好答案.但是,您可能应该包括开发人员在用户选择和编制索引后如何实施操作. (4认同)
  • 哈哈,因为大多数提问者不知道如何开始使用 CoreSpotlight 搜索 API。这里只是一个简单的指南。 (2认同)
  • 我不是在开玩笑。这是一个很好的答案,但缺少实现此问题的真实答案的要素。包括将来的问题寻求者将是很好的。 (2认同)
  • @DevC是的,你是对的!兼容性框架应该是弱连接的! (2认同)

Ayu*_*ush 13

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");
}];
Run Code Online (Sandbox Code Playgroud)

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


Uma*_*que 8

要完成Spotlight搜索功能,一旦实现mayqiyue的答案,你就可以看到在搜索,但对结果的选择就是你的应用程序将打开不相关的内容相关的查看结果.

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

 -(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;
        }
Run Code Online (Sandbox Code Playgroud)

确保在AppDelegate.m中导入:

 #import <MobileCoreServices/MobileCoreServices.h>
 #import <CoreSpotlight/CoreSpotlight.h>
Run Code Online (Sandbox Code Playgroud)

更新Swift 2.1

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

}
Run Code Online (Sandbox Code Playgroud)

确保在AppDelegate.swift中导入:

import CoreSpotlight
import MobileCoreServices
Run Code Online (Sandbox Code Playgroud)


sko*_*nos 7

我正在使用@mayqiyue提到的类似实现,但我也检查item变量是否存在,以便向后兼容iOS 8.

- (void)setupCoreSpotlightSearch
{
    CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];
    attibuteSet.title = NSLocalizedString(@"Be happy!", @"Be happy!");
    attibuteSet.contentDescription = @"Just like that";
    attibuteSet.keywords = @[@"example", @"stackoverflow", @"beer"];

    UIImage *image = [UIImage imageNamed:@"Image"];
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    attibuteSet.thumbnailData = imageData;

    CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"1"
                                                             domainIdentifier:@"album-1"
                                                                 attributeSet:attibuteSet];
    if (item) {
        [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"Search item indexed");
            }
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)

要处理从Spotlight搜索项目的点击,您需要在AppDelegate中实现以下方法:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
    if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {
        NSString *uniqueIdentifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];

        // Handle 'uniqueIdentifier'
        NSLog(@"uniqueIdentifier: %@", uniqueIdentifier);
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)