Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use NSUserActivity and CoreSpotlight but still set iOS8 as Deployment Target

Is it possible to use the new features of iOS9 such as NSUserActivity and CoreSpotlight, but still set my Development Target to 8.2 so that users with iOS8 can still use the app?

I assume I would just need to do a iOS version number check or use respondsToSelector:.

Is this correct?

like image 987
Nic Hubbard Avatar asked Mar 14 '23 15:03

Nic Hubbard


1 Answers

Yes, I do it in one of my apps (actually have a deployment target of iOS 7). It's trivial to do. Just make sure the CSSearchableIndex class exists, make the CoreSpotlight framework optional, and write your code properly to prevent the newer APIs from being run on devices with earlier versions of iOS.

You can even guard the code so it compiles under Xcode 6 if you had some reason to do so.

Example:

// Ensure it only compiles with the Base SDK of iOS 9 or later
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
    // Make sure the class is available and the device supports CoreSpotlight
    if ([CSSearchableIndex class] && [CSSearchableIndex isIndexingAvailable]) {
        dispatch_async(_someBGQueue, ^{
            NSString *someName = @"Some Name";
            CSSearchableIndex *index = [[CSSearchableIndex alloc] initWithName:someName];
            // rest of needed code to index with Core Spotlight
        });
    }
#endif

In your app delegate:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray *restorableObjects))restorationHandler {
    if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) {
        // This activity represents an item indexed using Core Spotlight, so restore the context related to the unique identifier.
        // The unique identifier of the Core Spotlight item is set in the activity’s userInfo for the key CSSearchableItemActivityIdentifier.
        NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier];
        if (uniqueIdentifier) {
            // process the identifier as needed
        }
    }

    return NO;
}
#endif
like image 190
rmaddy Avatar answered Apr 29 '23 01:04

rmaddy