核心聚焦目标c

时间:2016-04-23 21:08:16

标签: ios objective-c corespotlight

我正在尝试将Core Spotlight搜索添加到我的应用中。我已导入

#import <MobileCoreServices/MobileCoreServices.h>
#import <CoreSpotlight/CoreSpotlight.h>  

然后添加

[self setupCoreSpotlightSearch];

在viewDidLoad下。然后添加以下代码。

- (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");
    }
}

注意我已将我的包ID与.search一起添加。

当我尝试搜索地标的名称时,_locationTittle没有显示任何内容。我无法理解这一点,并试图遵循其他教程和示例代码。有人可以帮忙。

更新。这似乎是有效的,只需在按下时加载应用程序就好像应用程序只是正常加载一样。我将它添加到我的视图控制器中,以便在地标中查看更多细节。这意味着用户必须点击地标制作者才能开始出现在聚光灯下。理想情况下,我希望它显示所有的地标,而无需用户打开它们以便在聚光灯下显示,然后他们可以按下地标,以便加载特定的地标详细视图。在我的视图控制器中处理所有数据然后传递到我的地标的详细视图是这个代码,它将数据传递到详细信息视图:

- (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];

}

这一切都来自:

- (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;
}

如何使核心聚光灯工作,所以不必使用上面的代码为每个地标加载细节视图,当按下聚光灯搜索时,它会调用上面的代码?

0 个答案:

没有答案