是否可以使用iOS9的新功能,如NSUserActivity和CoreSpotlight,但仍然将开发目标设置为8.2,以便iOS8用户仍然可以使用该应用程序?
我想我只需要做一个iOS版本号检查或者使用respondsToSelector:。
这是正确的吗?
发布于 2015-09-30 21:26:30
是的,我是在我的一个应用程序中这样做的(实际上有一个iOS 7的部署目标)。做这件事很琐碎。只需确保CSSearchableIndex类存在,使CoreSpotlight框架可选,并正确编写代码,以防止在具有早期版本iOS的设备上运行较新的API。
您甚至可以保护代码,以便在Xcode 6下编译,如果您有理由这样做的话。
示例:
// 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在您的应用程序代表中:
#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;
}
#endifhttps://stackoverflow.com/questions/32875794
复制相似问题