国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS9中新API--Spotlight的使用詳細解析

2020-02-19 15:55:24
字體:
來源:轉載
供稿:網友

  下面是小編給大家分享的一篇iOS9中新API--Spotlight的使用詳細解析,感興趣的朋友跟小編一起來了解一下吧!

  1.Spotloight是什么?

  Spotlight在iOS9上做了一些新的改進, 也就是開放了一些新的API, 通過Core Spotlight Framework你可以在你的app中集成Spotlight。集成Spotlight的App可以在Spotlight中搜索App的內容,并且通過內容打開相關頁面。

  Demo演示

  2.如何集成Spotlight

  a.添加所需要的框架

  復制代碼 代碼如下:

  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000

  #import

  #import

  #endif

  注,很多APP都是支持iOS9以下的,因此加入#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000,可以解決iOS9以下設備運行崩潰的問題

  b.創建CSSearchableItemAttributeSet 對象

  復制代碼 代碼如下:

  CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

  attributeSet.title = spotlightTitle; // 標題

  attributeSet.keywords = keywords; // 關鍵字,NSArray格式

  attributeSet.contentDescription = spotlightDesc; // 描述

  attributeSet.thumbnailData = photo; // 圖標, NSData格式

  // 把圖片轉換成NSData的方法

  UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]

  c.創建可檢索條目CSSearchableItem

  復制代碼 代碼如下:

  // spotlightInfo 可以作為一些數據傳遞給接受的地方

  // domainId id,通過這個id來判斷是哪個spotlight

  CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet];

  d.添加檢索入口

  復制代碼 代碼如下:

  [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) {

  if (error) {

  NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);

  }

  }];

  ========完整代碼========

  復制代碼 代碼如下:

  - (void)insertSearchableItem:(NSData *)photo spotlightTitle:(NSString *)spotlightTitle description:(NSString *)spotlightDesc keywords:(NSArray *)keywords spotlightInfo:(NSString *)spotlightInfo domainId:(NSString *)domainId {

  CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

  attributeSet.title = spotlightTitle; // 標題

  attributeSet.keywords = keywords; // 關鍵字,NSArray格式

  attributeSet.contentDescription = spotlightDesc; // 描述

  attributeSet.thumbnailData = photo; // 圖標, NSData格式

  // spotlightInfo 可以作為一些數據傳遞給接受的地方

  // domainId id,通過這個id來判斷是哪個spotlight

  CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:spotlightInfo domainIdentifier:domainId attributeSet:attributeSet];

  [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * error) {

  if (error) {

  NSLog(@"indexSearchableItems Error:%@",error.localizedDescription);

  }

  }];

  }

  ========加載本地圖片的使用方法========

  復制代碼 代碼如下:

  [self insertSearchableItem:UIImagePNGRepresentation([UIImage imageNamed:@"xxx.png"]) spotlightTitle:@"等風來" description:@"等風來描述" keywords:@[@"鮑鯨鯨",@"大麗花"] spotlightInfo:@"傳遞過去的值" domainId:@"com.wb.spotlight"];

  ========加載網絡圖片的使用方法========

  復制代碼 代碼如下:

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

  NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://hiphotos.baidu.com/doc/pic/item/eaf81a4c510fd9f905f61934262dd42a2934a48e.jpg"]];

  [self insertSearchableItem:data spotlightTitle:@"等風來" description:@"等風來描述" keywords:@[@"鮑鯨鯨",@"大麗花"] spotlightInfo:@"傳遞過去的值" domainId:@"com.wb.spotlight"];

  });

  ========刪除所有spotlight的方法========

  復制代碼 代碼如下:

  [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {

  if (error) {

  NSLog(@"%@", error.localizedDescription);

  }

  }];

  ========刪除指定的spotlight的方法========

  復制代碼 代碼如下:

  [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:@"domainId" completionHandler:^(NSError * _Nullable error) {

  if (error) {

  NSLog(@"%@", error.localizedDescription);

  }

  }];

  ========點擊spotlight后的響應方法========

  復制代碼 代碼如下:

  - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {

  if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) {

  NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier];

  // 接受事先定義好的數值,如果是多個參數可以使用把json轉成string傳遞過來,接受后把string在轉換為json

  NSLog(@"傳遞過來的值%@", uniqueIdentifier);

  }

  return YES;

  }

  備注:

  復制代碼 代碼如下:

  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000

  // 相關spotlight的方法等

  #endif

  // Spotlight支持iOS9以上設備運行,對與低版本的設備需加入這個防止崩潰問題

  以上就是關于iOS9中新API--Spotlight的使用詳細解析了,更多相關內容請繼續關注武林技術頻道。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江山市| 定襄县| 贺兰县| 杭锦旗| 大安市| 曲阜市| 马龙县| 梨树县| 德安县| 铜川市| 桦川县| 凯里市| 哈密市| 阿巴嘎旗| 津市市| 图们市| 紫阳县| 甘德县| 广饶县| 沁阳市| 哈巴河县| 太仓市| 湄潭县| 平泉县| 福鼎市| 渝中区| 枣强县| 堆龙德庆县| 全南县| 天长市| 鄄城县| 汕头市| 南靖县| 旅游| 韶关市| 石河子市| 广东省| 临西县| 桐城市| 平潭县| 邯郸县|