0
votes

I have created a searchable NSUserActivity in iOS 9 as follows.

  userActivity = [[NSUserActivity alloc] initWithActivityType:@"com.example.app.conversation"];
    userActivity.title = @"Conversation with Fred Thompson";
    **userActivity.keywords = [NSSet setWithObjects:@"Holiday" @"Summer", nil];**
    userActivity.userInfo = @{@"keyOne":@"valueOne", @"keyTwo":@"valueTwo"};
    userActivity.eligibleForSearch = YES;
    userActivity.eligibleForPublicIndexing = NO;
    userActivity.eligibleForHandoff = NO;
    userActivity.contentAttributeSet = attributeSet;
    [userActivity becomeCurrent];

and I have registered the following activity type in my info.plist:

com.example.app.conversation

In Spotlight, I can search for any words of the TITLE and find my UserActivity; however, none of the KEYWORDS work for finding my UserActivity.

So, search for "Fred Thompson" will return the activity because those words are in the title; however search for "Holiday Summer" will return no results. (Note: that keywords are working for me with CSSearchableItems). Yes, I've also tried using obscure words just to ensure my results are not getting drowned out by other more popular results.

However, in no case, can I find my NSUSerActivity by the provided keywords. What am I doing wrong?

1
The NSUserActivity results don't work because the activity is getting released before it indexes. To make it to work, retain that property. self.userActivity = activity // make the userActivity to that of current ViewCrtl's activity. Also, make sure you have added CoreSpotlight and MobileCoreServices framework to your project. There is a nice tutorial on Spotlight : code.tutsplus.com/tutorials/…Vacca
You could try setting the contentAttributeSet property of the user activity with a CSSearchableItemAttributeSet instance (which has another keywords property. Note that you need to import CoreSpotlight in order to see the contentAttributeSet property on NSUserActivity.hagi

1 Answers

0
votes

Proved @hagi's answer is correct. Tested following code in iOS 9.3.1:

extension YourObject {
static let domainIdentifier = "com.example"

var userActivityUserInfo: [NSObject: AnyObject] {
    return ["hashID": "(hashID!)"]
}

var userActivity: NSUserActivity {
    let activity = NSUserActivity(activityType: YourObject.domainIdentifier)
    activity.title = title
    activity.userInfo = userActivityUserInfo
    activity.contentAttributeSet = attributeSet
    return activity
}

var attributeSet: CSSearchableItemAttributeSet {
    let attributeSet = CSSearchableItemAttributeSet(
        itemContentType: kUTTypeContact as String)
    attributeSet.title = title
    attributeSet.contentDescription = "Blah"

    if let data = thunbnailImageData {
        attributeSet.thumbnailData = data
    }
    attributeSet.supportsPhoneCall = false
    attributeSet.keywords = tags!

    return attributeSet
}

}