【Swift】iOS 9 Core Spotlight

來源:互聯網
上載者:User

標籤:

 

前言

  感覺 Spotlight 這個功能還是蠻有用的,能提升使用者活躍,增加應用內容曝光幾率。

 

聲明
  歡迎轉載,但請保留文章原始出處:)
  部落格園:http://www.cnblogs.com
  農民伯伯: http://over140.cnblogs.com

 

本文

  一、實現(iOS 9.0)

    1.1  添加索引

                            var searchableItems = [CSSearchableItem]()                            for app in apps {                                let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)                                searchableItemAttributeSet.title = app.label                                searchableItemAttributeSet.contentDescription = "Watch \(app.label) LIVE on Shou"                                let searchableItem = CSSearchableItem(uniqueIdentifier: app.id, domainIdentifier: "youappbundlerid.apps", attributeSet: searchableItemAttributeSet)                                                                searchableItems.append(searchableItem)                            }                            CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems, completionHandler: { (error: NSError?) in                                if error == nil {                                    print("initSpotlight...completed")                                } else {                                    print("\(error)")                                }                            })

    一定要注意把上面代碼加到子線程裡面執行,略慢。可以在 AppDelegate 裡面從伺服器請求完資料再加索引的。

    1.2  接收點擊事件

    @available(iOS 8.0, *)    func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {        if #available(iOS 9.0, *) {            if userActivity.activityType == CSSearchableItemActionType {                if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String, let label = userActivity.title {                    // 頁面跳轉                }            }        }        return true    }

 

  二、額外經驗

    2.1  CSSearchableItemAttributeSet

      可以不設定 thumbnailData ,這樣預設就是當前應用的 LOGO;另外注意 thumbnailURL 並不是用來指定網狀圖片地址的 - - #,所以你要加縮圖的話需要先把網上的下載到本地再設定

    2.2  CSSearchableItem

    預設失效是一個月,另外發現無法攜帶更多資料(比如通過 CSSearchableItemAttributeSet 的 setValue ,在 userActivity 那邊都接收不到)

 

    2.3  沒有找到索引條數的限制

    http://stackoverflow.com/questions/35284223/is-there-a-limit-for-number-of-items-cssearchableitem-in-core-spotlight-cssear

    我發現添加索引還是挺慢的,另外那個批量索引沒弄懂,感覺應該是又有刪除又有新增的話適合批量處理。

 

    2.4  沒用找到重新索引的方法

    重複調用也沒用問題,只要 uniqueIdentifier 匹配會覆蓋老的資料

 

    2.5  沒有找到提升排名的方法

    好像系統會有一套演算法來自動提升排名。

 

【Swift】iOS 9 Core Spotlight

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.