自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS 9:快速讓你的APP支持spotlight搜索

移動開發(fā)
iOS9中支持為app中的內(nèi)容做索引以支持spotlight搜索,感覺是個很有新意的功能。需要提到的是這些索引是存在本地設(shè)備中的,不會同步到icloud中,更換了設(shè)備就沒有了。

[[151508]]

iOS9中支持為app中的內(nèi)容做索引以支持spotlight搜索,感覺是個很有新意的功能。需要提到的是這些索引是存在本地設(shè)備中的,不會同步到icloud中,更換了設(shè)備就沒有了。

效果就是這樣:

blob.png

創(chuàng)建支持搜索的內(nèi)容

支持搜索的內(nèi)容的類是CSSearchableItem。

可以展示的屬性有標(biāo)題,一段描述文字,還有縮略圖。這里建議給每個item設(shè)置一個過期時間(expirationDate)。

blob.png

首先創(chuàng)建表示一個配置展示內(nèi)容的對象CSSearchableItemAttributeSet


  1. let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeData as String) 
  2. // Add metadata that supplies details about the item. 
  3. attributeSet.title = "July Report.Numbers" 
  4. attributeSet.contentDescription = "iWork Numbers Document" 
  5. attributeSet.thumbnailData = DocumentImage.jpg 

attributeSet也支持電話號碼,和地理坐標(biāo)。右邊會有對應(yīng)的動作,如果是支持導(dǎo)航會有一個箭頭。(因為我的app里不需要這功能,我自己沒有試過)


  1. attributeSet.phoneNumbers; 
  2. attributeSet.latitude; 
  3. attributeSet.longitude; 

blob.png

創(chuàng)建CSSearchableItem

uniqueIdentifier相當(dāng)于這條數(shù)據(jù)的id。domainIdentifier則表示相關(guān)的域。蘋果還提供了一組api對這些索引進行修改刪除操作,domainIdentifier可以當(dāng)做參數(shù),比如可以講一個域下的所有索引刪除。


  1. let item = CSSearchableItem(uniqueIdentifier: "1", domainIdentifier: "file-1", attributeSet: attributeSet) 

將CSSearchableItem添加至系統(tǒng)


  1. CSSearchableIndex.defaultSearchableIndex().indexSearchableItems([item]) { error in 
  2.   if error != nil { 
  3.      print(error?.localizedDescription) 
  4.   } 
  5.      else {  
  6.          print("Item indexed."
  7.   } 

tips

大部分的app可能還要兼容iOS8,這里介紹下swift下的判斷方法。

如果是在一個方法里要使用iOS9的api,使用下面來判斷(xcode7也會提醒你)。


  1. if #available(iOS 9.0, *) { 

如果是自己寫的一整個方法想表示只在iOS9可用,用下面的關(guān)鍵字加在方法頭表示


  1. @available(iOS 9.0, *) 

直接貼我的項目代碼了。

blob.png

blob.png

用戶搜索后選中打開app的處理

在app delegate里添加這個回調(diào)就好了。


  1. func application(UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: [AnyObject]? -> Void) -> Bool { 

在OC下的方法是這個


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

通過這個可以取到創(chuàng)建CSSearchableItem時設(shè)置的identifier


  1. NSString* identifier=userActivity.userInfo[CSSearchableItemActivityIdentifier]; 

接著就可以用這個identifier取出對應(yīng)的數(shù)據(jù),進行處理了。

參考鏈接:

責(zé)任編輯:倪明 來源: CocoaChina
相關(guān)推薦

2015-08-20 09:00:23

ios9api

2015-07-17 11:12:43

IOS搜索API

2013-04-16 16:01:51

APP支持iPhoneiOS開發(fā)

2020-08-26 19:24:51

iOS 14App Attest防護

2015-08-20 09:19:46

ios9uistackview

2013-07-12 13:24:19

App誘惑

2013-07-05 13:48:47

App

2018-11-19 15:06:23

Python算法

2012-01-05 09:19:25

iOSApp應(yīng)用

2015-09-22 10:59:45

iOS 9功能

2018-04-24 10:05:13

Docker工具交付

2013-09-18 15:56:18

Testin王軍App

2013-07-30 15:24:53

App應(yīng)用體驗

2015-09-16 09:02:10

IOS9新功能搜索業(yè)務(wù)

2020-11-20 10:40:20

PyTorch神經(jīng)網(wǎng)絡(luò)代碼

2012-07-09 11:20:59

2016-03-02 17:55:03

app用戶加載

2025-03-28 03:20:00

MySQL數(shù)據(jù)庫搜索

2013-02-01 17:36:35

App開發(fā)者用戶體驗

2013-11-25 11:25:05

產(chǎn)品設(shè)計App設(shè)計產(chǎn)品經(jīng)理
點贊
收藏

51CTO技術(shù)棧公眾號