适用于 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 ❤️