当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Core Spotlight objective c

问题描述:

I am trying to add Core Spotlight search to my app. I have imported

#import <MobileCoreServices/MobileCoreServices.h>

#import <CoreSpotlight/CoreSpotlight.h>

then added

[self setupCoreSpotlightSearch];

under viewDidLoad. Then added the following code.

- (void)setupCoreSpotlightSearch

{

if ([CSSearchableIndex isIndexingAvailable]) {

NSLog(@"Spotlight indexing is available on this device");

CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

// Set properties that describe attributes of the item such as title, description, and image.

NSString *title = _locationTittle;

attributeSet.title = title;

attributeSet.contentDescription = [NSString stringWithFormat:@"%@",title];

attributeSet.keywords = @[ title ];

// Create an attribute set for an item

UIImage *image = _locationImage;

NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

attributeSet.thumbnailData = imageData;

// Create a searchable item, specifying its ID, associated domain, and the attribute set you created earlier.

CSSearchableItem *item;

NSString *identifier = [NSString stringWithFormat:@"%@",attributeSet.title];

item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:@"com.mybundleid.productname.search" attributeSet:attributeSet];

// Index the item.

[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {

if (!error) {

NSLog(@"error = %@", error);

}];

} else {

NSLog(@"Spotlight indexing is not available on this device");

}

}

Note I have added my bundle id with .search after it.

When I try to search for the name of the landmark, which is _locationTittle nothing shows up. I cannot understand this and have tried to follow other tutorial and example code. Can someone please help.

Update. This seems to be working and just loads the app when pressed as if the app was just normally loaded. I added it into my view controller for looking in more detail at the landmark. This means that the user has to have clicked the landmark maker for it to start showing up in spotlight. Ideally I would like it to show all the landmarks without the user having to open them all for them to show in spotlight, then they can press the landmark for that specific landmarks detail view to be loaded. In my view controller where all the data is handled then passed to my detail view of the landmark is this code, which passes the data into the detail view:

- (void)locationClicked:(ARGeoCoordinate *)coordinate{

NSLog(@"%@", coordinate);

[Answers logCustomEventWithName:coordinate.locationTittle

customAttributes:@{}];

//user has tap on location, we need to show the location details view here

detailViewController *vc = (detailViewController*)[[self storyboard] instantiateViewControllerWithIdentifier:@"detailViewController"];

vc.locationImage = coordinate.imgLocation;

vc.locationTittle = coordinate.locationTittle;

vc.locationTxt = coordinate.locationTxt;

vc.PhotoCreditText = coordinate.photoCreditTittle;

[self.navigationController pushViewController:vc animated:YES];

}

This all comes from:

- (NSMutableArray *)geoLocations {

NSMutableArray *locationArray = [[NSMutableArray alloc] init];

ARGeoCoordinate *tempCoordinate;

CLLocation *tempLocation;

UIImage *locationImage;

// test locations

tempLocation = [[CLLocation alloc] initWithLatitude:53.712371 longitude:-1.882742];

tempCoordinate = [ARGeoCoordinate coordinateWithLocation:tempLocation locationTitle:@"Wainhouse Tower"];

locationImage = [UIImage imageNamed:@"WainhouseTower.jpg"];

[tempCoordinate setImgLocation:locationImage];

[tempCoordinate setLocationTxt:@"Wainhouse Tower is a folly in the parish of King Cross. At 275 feet (84 m), it is the tallest structure in Calderdale and the tallest folly in the world, and was erected in the four years between 1871 and 1875. The tower was completed on 9 September 1875, at a cost of £14,000. The main shaft is octagonal in shape and it has a square base and 403 steps leading to the first of two viewing platforms.\n\nOne driving force behind the erection of the viewing platforms was a long standing feud between Wainhouse and his neighbour, landowner Sir Henry Edwards. Edwards had boasted that he had the most private estate in Halifax, into which no one could see. As the estate was on land adjacent to the chimney's site, following the opening of the viewing platforms, Edwards could never claim privacy again.\n\nThe tower was designed by architect Isaac Booth as a chimney to serve the dye works owned by John Edward Wainhouse (1817–1883). The height of the chimney was to satisfy the Smoke Abatement Act of 1870 which required a tall chimney to carry smoke out of the valleys in which the factories were built. A much simpler chimney would have satisfied the requirements but Wainhouse insisted that it should be an object of beauty.\n\nIn 1874 John Wainhouse sold the mill to his works manager who refused to pay the cost of the chimney's construction so Wainhouse kept the tower for himself and used it as an observatory. Booth left after a dispute and was replaced by another local architect, Richard Swarbrick Dugale, who is responsible for the elaborate galleries and the corona dome at the top.\n\nThe tower is open to the public during bank holidays, and is a Grade II listed building.\n\n\nMaterial has been used from the Wikipedia article http://en.wikipedia.org/wiki/Wainhouse_Tower see that article's history for attribution.\n\nWainhouse Tower by James Preston https://flic.kr/p/cCMB2A is licensed under CC BY 2.0"];

[tempCoordinate setPhotoCreditTittle:@"See attribution below"];

[tempCoordinate setLocationTittle:@"Wainhouse Tower"];

[locationArray addObject:tempCoordinate];

return locationArray;

}

How do I make it so core spotlight works so the detail view doesnt have to have been loaded for each landmark by using the above code, and when the spotlight search is pressed it calls the above code?

分享给朋友:
您可能感兴趣的文章:
随机阅读: