適用於 iOS 的 React Native 模組,提供 Spotlight 搜尋功能。這允許您對 React Native 應用程式中的內容進行索引,以便它出現在 iOS 裝置的 Spotlight 搜尋索引中,從而可能增加應用程式的曝光度。
使用yarn(建議): $ yarn add react-native-spotlight-search
或使用 NPM: $ npm install react-native-spotlight-search --save
該套件無法在“Expo Go”應用程式中使用,因為它需要自訂本機程式碼。
安裝此 npm 套件後,將設定插件新增至app.json
或app.config.js
的plugins
陣列中:
{
"expo" : {
"plugins" : [ " react-native-spotlight-search " ]
}
}
接下來,請按照「新增自訂本機程式碼」指南中的說明重建您的應用程式。
自動連結或手動下方
react-native link react-native-spotlight-search
或手動下面
只需將RCTSpotlightSearch.xcodeproj
加入Libraries並將libRCTSpotlightSearch.a
加入Build Phases下的Link Binary With Libraries即可。有關如何執行此操作的更多資訊和螢幕截圖,請參閱 React Native 文件。
如果您希望能夠處理搜尋項目點擊的回調,則需要將以下程式碼新增至您的 AppDelegate 檔案:
# import " RCTSpotlightSearch.h "
- ( BOOL )application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)( NSArray * _Nullable))restorationHandler {
[RCTSpotlightSearch handleContinueUserActivity: userActivity];
return YES ;
}
如果 Xcode 抱怨無法找到頭文件,請確保您的專案的頭搜尋包含以下內容:
$(SRCROOT)/../node_modules/react-native-spotlight-search
像這樣:
首先,導入模組:
import SpotlightSearch from "react-native-spotlight-search" ;
您可以新增項目數組:
SpotlightSearch . indexItems ( [
{
title : "Strawberry" ,
contentDescription : "A sweet and juicy fruit." ,
uniqueIdentifier : "1" ,
domain : "fruit" ,
thumbnailName : "strawberry" ,
} ,
{
title : "Kiwi" ,
contentDescription : "Not a type of bird." ,
uniqueIdentifier : "2" ,
domain : "fruit" ,
thumbnailName : "kiwi" ,
} ,
] ) ;
或個別項目:
SpotlightSearch . indexItem ( {
title : "Strawberry" ,
contentDescription : "A sweet and juicy fruit." ,
uniqueIdentifier : "1" ,
thumbnailName : "strawberry" ,
} ) ;
財產 | 描述 | 類型 | 必需的 |
---|---|---|---|
title | 搜尋項的標題。 | string | 是的 |
contentDescription | 顯示在搜尋結果標題下方的描述。 | string | 不 |
uniqueIdentifier | 唯一且穩定的識別碼。用於指稱該項目。 | string | 是的 |
domain | 用於以有意義的方式將相關項目分組在一起的字串。不向使用者顯示。 | string | 是的 |
thumbnailName | 縮圖的本機檔案名稱/密鑰。請參閱有關縮圖的註釋。 | string | 不 |
thumbnailData | 縮圖圖片的 Base64 字串表示形式。請參閱有關縮圖的註釋。 | string | 不 |
keywords | 可用來幫助告知搜尋索引的關鍵字數組。對使用者不可見。 | [string] | 不 |
只需使用與新增項目相同的方法即可。請務必在為專案建立索引時引用相同的按鍵,以便任何新的元資料變更都會反映在 Spotlight 索引中。
可以透過標識符刪除項目:
SpotlightSearch . deleteItemsWithIdentifiers ( [ "1" , "2" ] ) ;
或按域:
SpotlightSearch . deleteItemsInDomains ( [ "fruit" ] ) ;
或者,您可以刪除應用程式索引的所有項目:
SpotlightSearch . deleteAllItems ( ) ;
所有 API 索引和刪除方法都是非同步的並傳回承諾。你可以像這樣連結事情:
SpotlightSearch . deleteAllItems ( ) . then ( ( ) => {
SpotlightSearch . indexItem ( {
title : "Strawberry" ,
contentDescription : "A sweet and juicy fruit." ,
uniqueIdentifier : "1" ,
thumbnailName : "strawberry" ,
} ) ;
} ) ;
您可以選擇新增自訂處理程序,當使用者點擊 Spotlight 結果中的搜尋項目之一時將呼叫該處理程序:
SpotlightSearch . searchItemTapped ( ( uniqueIdentifier ) => {
alert ( `You tapped on ${ uniqueIdentifier } !` ) ;
} ) ;
或者,如果您想要擷取點擊開啟應用程式的搜尋項目(可能在事件觸發後設定了偵聽器):
SpotlightSearch . getInitialSearchItem ( ) . then ( ( uniqueIdentifier ) => {
alert ( `You tapped on ${ uniqueIdentifier } and opened the app!` ) ;
} ) ;
// example in a useEffect with listener cleanup
useEffect ( ( ) => {
const spotlightListener = SpotlightSearch . searchItemTapped ( ( uniqueIdentifier ) => {
alert ( `You tapped on ${ uniqueIdentifier } and opened the app!` ) ;
} )
return ( ) => {
// cleanup listener
spotlightListener . remove ( )
}
} , [ ] )
此參數將是項目索引所使用的uniqueIdentifier
。您可以使用它來查找項目並顯示有關它的信息,例如透過導航到應用程式中的相關頁面。
目前,為了使用影像路徑,它必須存在於裝置本機的專案資源資料夾中。這是 iOS 的限制,而不是這個函式庫的限制。
若要使用不在資源資料夾中的映像(本機或遠端檔案),請將它們讀取為 base64 並使用thumbnailData
屬性包含字串值。
歡迎 PR ❤️