Skip to content

Instantly share code, notes, and snippets.

@mazkewich
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazkewich/3aec8de3fd4d35d06687 to your computer and use it in GitHub Desktop.
Save mazkewich/3aec8de3fd4d35d06687 to your computer and use it in GitHub Desktop.
Search API
//
// MRGPersonInformationViewController.m
// TestSearchAPI
//
// Created by Ричард Мацкевич on 7/8/15.
// Copyright © 2015 Ричард Мацкевич. All rights reserved.
//
@import CoreSpotlight;
@import MobileCoreServices;
#import "MRGPersonInformationViewController.h"
#import "MRGPerson.h"
@interface MRGPersonInformationViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *profilePicture;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseLabel;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;
@property (strong, nonatomic) NSUserActivity *userActivity;
@end
@implementation MRGPersonInformationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureInitialState];
// [self useCoreSpotlight];
[self useNSUserActivityAPI];
}
- (void)useNSUserActivityAPI
{
_userActivity = [[NSUserActivity alloc]
initWithActivityType:@"com.mazkewich.TestSearchAPI.browsing"];
_userActivity.title = self.person.fullName;
_userActivity.keywords = [NSSet setWithArray:@[self.person.fullName, self.person.houseName]];
_userActivity.userInfo = @{ @"id" : self.person.personalID };
_userActivity.eligibleForSearch = YES;
CSSearchableItemAttributeSet* attributeSet = [self createSearchableItemAttributeSet];
_userActivity.contentAttributeSet = attributeSet;
[_userActivity becomeCurrent];
}
- (void)useCoreSpotlight
{
CSSearchableItemAttributeSet* attributeSet = [self createSearchableItemAttributeSet];
CSSearchableItem *item;
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:self.person.personalID domainIdentifier:@"domain1.subdomainA" attributeSet:attributeSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
NSLog(@"Search item indexed");
}];
}
- (CSSearchableItemAttributeSet *)createSearchableItemAttributeSet
{
CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];
attributeSet.title = self.person.fullName;
attributeSet.contentDescription = [NSString stringWithFormat:@"%@ from %@'s house.\nHe is %@ now.", self.person.fullName, self.person.houseName, self.person.currentState];
attributeSet.thumbnailData = UIImagePNGRepresentation(self.person.image);
return attributeSet;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)configureInitialState
{
self.profilePicture.image = self.person.image;
self.nameLabel.text = self.person.fullName;
self.houseLabel.text = self.person.houseName;
self.stateLabel.text = self.person.currentState;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment