天天看點

iOS9系列專題二——全新的搜尋功能api

        ios9中為我們提供了許多新的api,搜尋功能的加強無疑是其中比較顯眼的一個。首先,我們先設想一下:如果在你的app中定義一種辨別符,在siri和搜尋中,可以用過這個辨別符搜尋到你的app,是不是很棒?不,這還差得遠,你可以定義任意的資料,使其在搜尋和siri中可以快速檢索到,這樣的搜尋功能是不是非常酷?不,還有更cool的,你甚至可以在你的網站中添加一些标志,使apple的爬蟲可以檢索到,那樣,即使使用者沒有安裝你的app,也可以在搜尋中擷取到相應的資訊,這太強大了,對吧。

        我們可以在項目中使用相應的函數來添加一些使用者的活躍元素,使我們可以在搜尋中通過搜尋這樣的活躍元素展現我們的app。例如:

<a href="http://my.oschina.net/u/2340880/blog/511694#">?</a>

1

2

3

4

5

6

7

8

9

10

<code>    </code><code>//建立一個對象,這裡的type用于區分搜尋的類型</code>

<code>    </code><code>nsuseractivity *useractivity = [[nsuseractivity alloc] initwithactivitytype: @</code><code>"myapp"</code><code>];</code>

<code>    </code><code>//顯示的标題</code>

<code>    </code><code>useractivity.title = @</code><code>"我的app"</code><code>;</code>

<code>    </code><code>// 搜尋的關鍵字</code>

<code>    </code><code>useractivity.keywords = [nsset setwitharray: @[@</code><code>"sea"</code><code>,@</code><code>"rch"</code><code>]];</code>

<code>    </code><code>// 支援search</code>

<code>    </code><code>useractivity.eligibleforsearch = yes;</code>

<code>    </code><code>//送出設定</code>

<code>    </code><code>[useractivity becomecurrent];</code>

在下面的函數中,我們可以處理使用者點選搜尋後的回調:

11

12

<code>- (</code><code>bool</code><code>)application:(uiapplication *)application continueuseractivity:(nsuseractivity *)useractivity restorationhandler:</code>

<code>{</code>

<code> </code><code>nsstring *activitytype = useractivity.activitytype;</code>

<code>    </code><code>if</code> <code>([activitytype isequal: @</code><code>"myapp"</code><code>]){</code>

<code>        </code><code>// handle restoration for values provided in userinfo</code>

<code>        </code><code>// do something</code>

<code>        </code><code>return</code> <code>yes;</code>

<code>    </code><code>}</code>

<code>    </code><code>return</code> <code>no;</code>

<code>    </code><code>//處理回調</code>

<code>}</code>

tip:這種方式添加的關鍵字搜尋,必須建立全局變量,否則無法進行搜尋:

iOS9系列專題二——全新的搜尋功能api

        corespotlight是一種更加自由的搜尋方式,可以通過添加類似item的模型,将app中的資料展示在搜尋欄中,corespotlight架構類似提供了一些增、删、改、查的操作,可是使我們自由的進行搜尋屬性的設定。

在ios9中,新增加了3個類,通過對這三個類的操作與配合,我們可以輕易的在app中添加corespotlight搜尋的功能。

 cssearchableitemattributeset:設定類,這個類用于設定搜尋标簽裡的icon,内容,圖檔等。主要用法如下:

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<code>//這個類的核心方法隻有一個init方法,通過一個類型字元串進行建立,字元串用于在回調中區分</code>

<code>@interface cssearchableitemattributeset : nsobject &lt;nscopying,nssecurecoding&gt;</code>

<code>- (instancetype)initwithitemcontenttype:(nonnull nsstring *)itemcontenttype;</code>

<code>@end</code>

<code>//更多的屬性設定在其擴充類中,例如:</code>

<code>@interface cssearchableitemattributeset (csgeneral)</code>

<code>//展示的名稱</code>

<code>@property(nullable, copy) nsstring *displayname;</code>

<code>//名稱數組</code>

<code>@property(nullable, copy) nsarray&lt;nsstring*&gt; *alternatenames;</code>

<code>//完整的路徑</code>

<code>@property(nullable, copy) nsstring *path;</code>

<code>//連結url</code>

<code>@property(nullable, strong) nsurl *contenturl;</code>

<code>//圖檔連結的url</code>

<code>@property(nullable, strong) nsurl *thumbnailurl;</code>

<code>//設定圖檔資料</code>

<code>@property(nullable, copy) nsdata *thumbnaildata;</code>

<code>//設定一個辨別符</code>

<code>@property(nullable, copy) nsstring *relateduniqueidentifier;</code>

<code>@property(nullable, strong) nsdate *metadatamodificationdate;</code>

<code>//内容類型</code>

<code>@property(nullable, copy) nsstring *contenttype;</code>

<code>@property(nullable, copy) nsarray&lt;nsstring*&gt; *contenttypetree;</code>

<code>//搜尋的關鍵字數組</code>

<code>@property(nullable, copy) nsarray&lt;nsstring*&gt; *keywords;</code>

<code>//标題資訊</code>

<code>@property(nullable, copy) nsstring *title;</code>

 cssearchableitem:搜尋标簽類,通過這個類,來建立響應的搜尋标簽。主要内容如下:

<code>//這個類主要用于建立搜尋的标簽</code>

<code>@interface cssearchableitem : nsobject &lt;nssecurecoding, nscopying&gt;</code>

<code>//init方法</code>

<code>- (instancetype)initwithuniqueidentifier:(nullable nsstring *)uniqueidentifier </code><code>//can be null, one will be generated</code>

<code>                        </code><code>domainidentifier:(nullable nsstring *)domainidentifier</code>

<code>                            </code><code>attributeset:(cssearchableitemattributeset *)attributeset;</code>

<code>//相應 的屬性</code>

<code>@property (copy) nsstring *uniqueidentifier;</code>

<code>@property (copy, nullable) nsstring *domainidentifier;</code>

<code>@property (copy, null_resettable) nsdate * expirationdate;</code>

<code>@property (strong) cssearchableitemattributeset *attributeset;</code>

cssearchableindex:這個類,我個人了解,類似一個manager的作用,通過它對标簽進行增、删、改、查等操作:

<code>@interface cssearchableindex : nsobject</code>

<code>@property (weak,nullable) id&lt;cssearchableindexdelegate&gt; indexdelegate;</code>

<code>//判斷裝置是否支援</code>

<code>+ (</code><code>bool</code><code>)isindexingavailable;</code>

<code>//取系統的searchindex管理者</code>

<code>+ (instancetype)defaultsearchableindex;</code>

<code>//一般情況下,我們不需要重新建立對象</code>

<code>- (instancetype)initwithname:(nsstring *)name;</code>

<code>- (instancetype)initwithname:(nsstring *)name protectionclass:(nullable nsstring *)protectionclass;</code>

<code>//設定索引标簽</code>

<code>- (</code><code>void</code><code>)indexsearchableitems:(nsarray&lt;cssearchableitem *&gt; *)items completionhandler:(</code><code>void</code> <code>(^ __nullable)(nserror * __nullable error))completionhandler;</code>

<code>//删除指定id索引标簽</code>

<code>- (</code><code>void</code><code>)deletesearchableitemswithidentifiers:(nsarray&lt;nsstring *&gt; *)identifiers completionhandler:(</code><code>void</code> <code>(^ __nullable)(nserror * __nullable error))completionhandler;</code>

<code>- (</code><code>void</code><code>)deletesearchableitemswithdomainidentifiers:(nsarray&lt;nsstring *&gt; *)domainidentifiers completionhandler:(</code><code>void</code> <code>(^ __nullable)(nserror * __nullable error))completionhandler;</code>

<code>//删除所有索引标簽</code>

<code>- (</code><code>void</code><code>)deleteallsearchableitemswithcompletionhandler:(</code><code>void</code> <code>(^ __nullable)(nserror * __nullable error))completionhandler;</code>

        下面,我們通過一個小例子來應用下corespotlight的搜尋功能。

首先,需要在項目中導入如下庫:

iOS9系列專題二——全新的搜尋功能api

實作如下代碼:

<code> </code><code>//進行标簽設定</code>

<code> </code><code>cssearchableitemattributeset * itemset = [[cssearchableitemattributeset alloc]initwithitemcontenttype:@</code><code>"myapp"</code><code>];</code>

<code>    </code><code>itemset.title = @</code><code>"我的app"</code><code>;</code>

<code>    </code><code>itemset.keywords = @[@</code><code>"haha"</code><code>,@</code><code>"123"</code><code>];</code>

<code>    </code><code>itemset.contentdescription = @</code><code>"這是搜尋到得内容"</code><code>;</code>

<code>    </code><code>itemset.thumbnaildata = uiimagepngrepresentation([uiimage imagenamed:@</code><code>"icon-114.png"</code><code>]);</code>

<code>    </code> 

<code>    </code><code>cssearchableitem * item = [[cssearchableitem alloc]initwithuniqueidentifier:@</code><code>"1"</code> <code>domainidentifier:@</code><code>"1"</code> <code>attributeset:itemset];</code>

<code>    </code><code>[[cssearchableindex defaultsearchableindex]indexsearchableitems:@[item] completionhandler:nil];</code>

我們在搜尋中輸入haha或者123效果如下:

iOS9系列專題二——全新的搜尋功能api

corespotlight的搜尋回調和nsuseractivaty一樣,隻是區分id的方式有所不同:

<code>    </code><code>nsstring *activitytype = useractivity.activitytype;</code>

<code>    </code><code>//先取cssearchableitemactiontype</code>

<code>    </code><code>if</code> <code>([activitytype isequal: cssearchableitemactiontype]) {</code>

<code>        </code><code>nsstring *uniqueidentifier = [useractivity.userinfo                                                 objectforkey:cssearchableitemactivityidentifier];</code>

        在我參考的許多相關文章中,都一緻建議,ios9的搜尋功能固然強大,然而濫用會造成垃圾資訊的泛濫,這樣的結果一定會适得其反,作為開發者,我們需要将最合适,最簡潔的資訊推送到使用者的面前。另外,文章有疏漏和錯誤之處,歡迎指正。

繼續閱讀