首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSUserActivity和CoreSpotlight,但仍将iOS8设置为部署目标

使用NSUserActivity和CoreSpotlight,但仍将iOS8设置为部署目标
EN

Stack Overflow用户
提问于 2015-09-30 21:17:19
回答 1查看 492关注 0票数 4

是否可以使用iOS9的新功能,如NSUserActivityCoreSpotlight,但仍然将开发目标设置为8.2,以便iOS8用户仍然可以使用该应用程序?

我想我只需要做一个iOS版本号检查或者使用respondsToSelector:

这是正确的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-30 21:26:30

是的,我是在我的一个应用程序中这样做的(实际上有一个iOS 7的部署目标)。做这件事很琐碎。只需确保CSSearchableIndex类存在,使CoreSpotlight框架可选,并正确编写代码,以防止在具有早期版本iOS的设备上运行较新的API。

您甚至可以保护代码,以便在Xcode 6下编译,如果您有理由这样做的话。

示例:

代码语言:javascript
复制
// 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

在您的应用程序代表中:

代码语言:javascript
复制
#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
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32875794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档