在 Spotlight 搜索中索引您的应用程序的一种简单方法
定义在索引搜索中使用的 App 图标。如果传递 "nil",则搜索将用 "AppIcon" 命名的图标进行索引
Podfile
pod "SpotlightHandler"
终端
pod install
第一步是使您的 ViewController 成为 BaseIndexableViewController 的子类,如下所示
#import "BaseIndexableViewController.h"
@interface ViewController : BaseIndexableViewController
@end
这样,Spotlight 设置就已在 Interface Builder 中可见
然后按照您偏好的方式填写字段
(别忘了开启“Spot Indexable”字段) =)
最后,在我们的 AppDelegate 类中添加一个方法,该方法将负责识别 Spotlight 的动作并显示选定的控制器
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {
NSString *uniqueIdentifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];
[SpotlightHandler openController:uniqueIdentifier];
}
return YES;
}