【问题标题】:trouble with iOS9 search API NSUserActivityiOS9 搜索 API NSUserActivity 的问题
【发布时间】:2015-09-08 08:01:23
【问题描述】:

我在看iOS9搜索API WWDC2015的视频时,有一个这样的demo:

var activity:NSUserActivity = NSUserActivity(activityType:"com.yummly.browseRecipe")
activity.title = "Baked Potato Chips"
activity.userInfo = ["id":"http://www.yummly.com/recipe/BPC-983195"]
activity.eligibleForSearch = true
activity.becomeCurrent()

我将此代码复制到我的 Xcode 并运行它,当我通过 Spotlight 搜索时,没有结果。它出什么问题了? iOS9 的错误?

【问题讨论】:

    标签: ios ios9


    【解决方案1】:

    由于某种原因,您必须在调用 activity.becomeCurrent() 后保留对 NSUserActivity 对象的引用。像这样(斯威夫特):

    activity.becomeCurrent()
    self.lastActivity = activity
    

    其中“lastActivity”是您所在班级的属性。

    【讨论】:

      【解决方案2】:

      我确实喜欢这个,是在 Objective-C 中,但你可以很容易地翻译成 swift

       if ([CSSearchableItemAttributeSet class]) {
      
      CSSearchableItemAttributeSet* attributes = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:@"kUTTypePackage"];//Set you content type
      
      attributes.title = model.name;
      attributes.contentDescription = model.description;
      attributes.identifier = model.fileURL.lastPathComponent;
      
      UIImage* backImage = [UIImage imageWithData:model.imageData];
      
      if (backImage == nil) {
      
          attributes.thumbnailData = UIImageJPEGRepresentation([UIImage imageNamed:@"DefaultImage"], 0.5);
      
      }else{
      
          attributes.thumbnailData = model.imageData;
      }
      
      NSString* domainID = @"com.myapp.mycompany";
      NSString* uniqueID = model.fileURL.lastPathComponent;
      CSSearchableItem* item = [[CSSearchableItem alloc]initWithUniqueIdentifier:uniqueID //value passed from NSUserActivity inside .userInfo
                                                                domainIdentifier:domainID
                                                                    attributeSet:attributes];
      
      NSLog(@"Item Attributes:%@",item.attributeSet.identifier);
      CSSearchableIndex* index = [CSSearchableIndex defaultSearchableIndex];
      [index indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
      
          if (!error) {
              NSLog(@"Item \"%@\" indexed",model.name);
          }else{
              NSLog(@"Error, \"%@\" not indexed: %@, %@",model.name,error, error.userInfo);
          }
      
      }];
      }else{
      
          NSLog(@"iOS < 9");
      }
      

      【讨论】:

      • 谢谢。您使用 Core Spotlight API 提供代码。我已经修好了。 NSUserActivity 对象不应立即释放。所以我把活动折腾成一个强属性:@property(nonatomic, Strong) NSUserActivity *activity;然后,它的工作原理。
      • @Totka 对不起这个问题,但是什么是模型对象?是你自己的数据模型吗?
      • @Beto 是的,这是我的数据模型
      【解决方案3】:

      您需要将 NSUserActivity 附加到控制器,如下所示。

      controller.userActivity = 活动

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多