是否有corepotlight搜索功能的示例代碼 - iOS 9 API?真的很感激,如果可以看看示例代碼來實現/測試。是否有corepotlight搜索功能的示例代碼 - iOS 9 API?
回答
CSSearchableItemAttributeSet *attributeSet;
attributeSet = [[CSSearchableItemAttributeSet alloc]
initWithItemContentType:(NSString *)kUTTypeImage];
attributeSet.title = @"My First Spotlight Search";
attributeSet.contentDescription = @"This is my first spotlight Search";
attributeSet.keywords = @[@"Hello", @"Welcome",@"Spotlight"];
UIImage *image = [UIImage imageNamed:@"searchIcon.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;
CSSearchableItem *item = [[CSSearchableItem alloc]
initWithUniqueIdentifier:@"com.deeplink"
domainIdentifier:@"spotlight.sample"
attributeSet:attributeSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item]
completionHandler: ^(NSError * __nullable error) {
if (!error)
NSLog(@"Search item indexed");
}];
注意:kUTTypeImage
要求您導入MobileCoreServices框架。
創建一個新的iOS項目,並添加CoreSpotlight和MobileCoreServices框架到您的項目。
創建實際的CSSearchableItem並關聯uniqueIdentifier,domainIdentifier和attributeSet。最後使用[[CSSearchableIndex defaultSearchableIndex] ...]對CSSearchableItem進行索引,如下所示。
行!測試索引!
很好的答案。但是,您應該包括開發人員在用戶選擇並編制索引後如何實施行動方案。 – soulshined
HAHA,因爲大多數提問者只是不知道如何從CoreSpotlight搜索API開始。這裏只是一個簡單的指南。 – mayqiyue
我並不是一個開玩笑的人。這是一個很好的答案,但沒有完成對這個問題的真實答案的要素。爲將來的問題提供者包括將是一件好事。 – soulshined
我使用類似的實現如由@mayqiyue提及,但我也檢查item
變量的向後兼容性的存在與iOS 8.
- (void)setupCoreSpotlightSearch
{
CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];
attibuteSet.title = NSLocalizedString(@"Be happy!", @"Be happy!");
attibuteSet.contentDescription = @"Just like that";
attibuteSet.keywords = @[@"example", @"stackoverflow", @"beer"];
UIImage *image = [UIImage imageNamed:@"Image"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attibuteSet.thumbnailData = imageData;
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"1"
domainIdentifier:@"album-1"
attributeSet:attibuteSet];
if (item) {
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Search item indexed");
}
}];
}
}
要在處理抽頭從Spotlight搜索項目,您需要在AppDelegate中執行以下方法:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {
NSString *uniqueIdentifier = userActivity.userInfo[CSSearchableItemActivityIdentifier];
// Handle 'uniqueIdentifier'
NSLog(@"uniqueIdentifier: %@", uniqueIdentifier);
}
return YES;
}
要完成聚光燈搜索功能,一旦您實施了mayqiyue's答案,您將能夠在搜索中看到結果,但在結果的選擇上,只是您的應用不會打開與相關內容相關的視圖。
爲了達到目的,請轉到您的AppDelegate.m並添加以下方法。
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
//check if your activity has type search action(i.e. coming from spotlight search)
if ([userActivity.activityType isEqualToString:CSSearchableItemActionType ] == YES) {
//the identifier you'll use to open specific views and the content in those views.
NSString * identifierPath = [NSString stringWithFormat:@"%@",[userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier]];
if (identifierPath != nil) {
// go to YOUR VIEWCONTROLLER
// use notifications or whatever you want to do so
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
// this notification must be registered in MyViewController
[[NSNotificationCenter defaultCenter] postNotificationName:@"OpenMyViewController" object: myViewController userInfo:nil];
return YES;
}
}
return NO;
}
確保進口在AppDelegate.m:
#import <MobileCoreServices/MobileCoreServices.h>
#import <CoreSpotlight/CoreSpotlight.h>
更新雨燕2.1
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
if #available(iOS 9.0, *) {
if userActivity.activityType == CSSearchableItemActionType {
//the identifier you'll use to open specific views and the content in those views.
let dict = userActivity.userInfo! as NSDictionary
let identifierPath = dict.objectForKey(CSSearchableItemActivityIdentifier) as! String
if identifierPath.characters.count > 0 {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mvc: MyViewController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! MyViewController
NSNotificationCenter.defaultCenter().postNotificationName("OpenMyViewController", object: mvc, userInfo: nil)
}
return true
}
} else {
// Fallback on earlier versions
return false
}
return false
}
確保進口在AppDelegate中。SWIFT:
import CoreSpotlight
import MobileCoreServices
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeImage as String)
attributeSet.title = "Searchable Item"
attributeSet.contentDescription = "Code for creating searchable item"
attributeSet.keywords = ["Item","Searchable","Imagine"]
attributeSet.thumbnailURL = NSURL(string: "https://blog.imagine.com/")
let searchableItem = CSSearchableItem(uniqueIdentifier: "com.imagine.objectA", domainIdentifier: "spotlight.search", attributeSet: attributeSet)
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([searchableItem]) {_ in}
你可以給你的答案添加一個解釋。謝謝 – Clarkie
寫在你的主控制器類
-(void)storeValueForSpotligtSearch { NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; // **Your Model Array that Contain Data Like attributes Make, Model, Variant and Year and Images** for (MyCatalogeModel *myCatalogeModelObj in yourDataContainer) { NSMutableArray *arrKeywords = [[NSMutableArray alloc] initWithObjects: myCatalogeModelObj.year, myCatalogeModelObj.make, myCatalogeModelObj.model, myCatalogeModelObj.variant, nil]; NSString *strIdentifier = [NSString stringWithFormat:@"%@.%@",bundleIdentifier, myCatalogeModelObj.carId]; self.userActivity = [[NSUserActivity alloc]initWithActivityType:strIdentifier]; self.userActivity.title = myCatalogeModelObj.year; self.userActivity.title = myCatalogeModelObj.make; self.userActivity.title = myCatalogeModelObj.model; self.userActivity.title = myCatalogeModelObj.variant; self.userActivity.eligibleForSearch = YES; self.userActivity.eligibleForPublicIndexing = YES; self.userActivity.eligibleForHandoff = YES; CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeJSON]; attributeSet.title = myCatalogeModelObj.make; attributeSet.thumbnailData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[myCatalogeModelObj.imageArray objectAtIndex:0]]]; attributeSet.contentDescription = [NSString stringWithFormat:@"%@ %@ %@ %@", myCatalogeModelObj.year, myCatalogeModelObj.make, myCatalogeModelObj.model, myCatalogeModelObj.variant]; attributeSet.keywords = arrKeywords; CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:strIdentifier domainIdentifier:@"spotlight.CARS24ChannelPartnerapp" attributeSet:attributeSet]; [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) { }]; self.userActivity.contentAttributeSet = attributeSet; [self.userActivity becomeCurrent]; [self updateUserActivityState:self.userActivity]; } }
寫在應用程序委託
-(BOOL)application:(nonnull UIApplication *) application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * __nullable))restorationHandler { @try { NSString *strIdentifier; NSNumber *numScreenId; NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init]; NSLog(@"Activity = %@",userActivity.userInfo); if (userActivity.userInfo[@"vc"]) { numScreenId = userActivity.userInfo[@"vc"]; } else{ strIdentifier = [userActivity.userInfo objectForKey:@"kCSSearchableItemActivityIdentifier"]; NSLog(@"strIdentifier : %@",strIdentifier); NSArray *arr = [strIdentifier componentsSeparatedByString:@"."]; NSString *strScreenId = [arr objectAtIndex:3]; NSLog(@"ID -= %@",strScreenId); **// On Click in Spotlight search item move your particular view.** [self moveToParticular:[strScreenId intValue]]; numScreenId = [numFormatter numberFromString:strScreenId]; } } @catch (NSException *exception) {} return YES; }
@ PeterPan666謝謝 –
- 1. C代碼,搜索功能
- 2. WF4是否有能力搜索實例?
- 3. sourceforge.net是否有搜索API?
- 4. 是由搜索引擎索引的代碼示例?
- 5. 搜索功能解析IOS
- 6. Spotify iOS搜索功能
- 7. 搜索和碼功能格式取代
- 8. c#搜索功能查詢/代碼
- 9. 是否有類似於「Twitter搜索API」的「Facebook搜索API」?
- 10. ruby中的功能代碼示例
- 11. 代碼示例功能的腳本
- 12. 搜索功能! JS API
- 13. BOX API v2.0搜索功能
- 14. Twitterizer - 搜索API示例
- 15. KML是否支持搜索功能
- 16. 是否有Google App搜索的REST API?
- 17. 我的代碼沒有搜索功能的作品
- 18. 搜索功能與聯網ios
- 19. 是否有WIN32註冊表搜索功能?
- 20. 是否可以分離Eclipse的代碼索引功能?
- 21. 是否有Google自定義搜索休息API的工作示例?
- 22. 是否有iPhone的QR碼掃描任何API或示例代碼
- 23. 搜索功能
- 24. 是否有一個API來檢測iOS上的CPU功能?
- 25. 是否有適用於iOS輔助觸摸功能的API?
- 26. 搜索功能沒有真正搜索
- 27. 微博是否有v2搜索API?
- 28. 是否有博客搜索API?
- 29. 是否有Magento CE內置的精煉搜索功能?
- 30. Rails 3.0 - ActiveRecord類中是否有內置的搜索功能?
縮略圖自Beta 2以來的工作 – KIDdAe
感謝您的代碼段。 –
看起來不對。 ''com.deeplink''實際上應該是一個唯一的標識符,它唯一標識了你正在建立索引的對象(比如數據庫中的主ID,GUUID或其他)。 CSSearchableItemAttributeSet上的'kUTTypeImage'是您需要設置深層鏈接的地方。蘋果在主題演講中展示的例子實際上是錯誤的,並不一致,但正確的例子可以在視頻的前面看到。 – strangetimes