12

以下是我尝试实现的代码,以使应用程序活动和状态可搜索但无法在 iOS 搜索中显示

NSUserActivity *userActivity = [[NSUserActivity alloc]initWithActivityType:@"com.mycompany.activity-type"];

userActivity.title = @"Hello world from in app search";
userActivity.keywords = [NSSet setWithArray:@[@"Hello",@"Welcome", @"search"]];
userActivity.userInfo = @{@"id":@"com.example.state"};

userActivity.eligibleForSearch = YES;    
[userActivity becomeCurrent];

链接使我的问题更清楚。

4

4 回答 4

22

来自苹果论坛:

咬了几个人(包括我自己)的一件事是不能释放活动。如果您的代码仅与 NSUserActivities 一起使用(即不使用 CoreSpotlight),请确保 您的活动没有被立即释放
在我的例子中,我有分配 NSUA 的代码,在其上设置一些属性,调用 becomeCurrent,但随后该对象将超出范围并被释放。如果您正在这样做,请尝试将活动放入一个强大的属性中,看看您是否可以在搜索时看到结果。

https://forums.developer.apple.com/message/13640#13640

于 2015-07-18T22:46:29.987 回答
6

我发现你必须在调用 -becomeCurrent 之前将你创建的 NSUserActivity 实例分配给你当前可见的 UIViewControllers 的 userActivity 属性。它已经为我修复了它,并且这些项目立即出现在其他设备上的切换和同一设备上的聚光灯搜索中。

于 2015-06-18T19:48:59.170 回答
0

我遇到了同样的问题,我在开发论坛上读到,在种子 1 中它只能在设备上运行。我能够让它在设备上工作。

这可能与切换一样,只能在设备上运行。

于 2015-06-19T00:29:53.353 回答
-2

我也无法让它与 beta 2 一起使用。使用CSSearchableItemAttributeSet

CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString*)kUTTypeImage];
attributeSet.title = myobject.title;
attributeSet.keywords = [myobject.desc componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
attributeSet.contentDescription = myobject.desc;
if (myobject.images.count > 0) {
    attributeSet.thumbnailData = myobject.myimagedata;
}
attributeSet.rating = @(myobject.rating.integerValue / 2);

CSSearchableItem* item;
item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"..." domainIdentifier:@"..." attributeSet:attributeSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
    NSLog(@"Search item indexed");
}];

但是,即使使用图像也可以。我无法开始工作的是评级显示在任何地方。

于 2015-07-06T16:41:30.740 回答