react native search api
1.0.0
SearchApi
模块为您提供了一个通用的 React Native 接口来与 iOS 搜索 API、Core Spotlight 进行交互。
有关 iOS 搜索 API 的更多信息,请参阅 https://developer.apple.com/ios/search/。
npm install react-native-search-api --save
react-native link
在AppDelegate.m
的顶部添加以下行:
# import " RCTSearchApiManager.h "
在您的 AppDelegate 实现中添加以下内容:
- ( BOOL )application:(UIApplication *)application continueUserActivity:( NSUserActivity *)userActivity restorationHandler:( void (^)( NSArray * _Nullable))restorationHandler {
return [RCTSearchApiManager application: application continueUserActivity: userActivity restorationHandler: restorationHandler];
}
订阅组件中的搜索项打开事件,如下所示:
componentDidMount ( ) {
< ... >
SearchApi . addOnSpotlightItemOpenEventListener ( this . handleOnSpotlightItemOpenEventListener ) ;
SearchApi . addOnAppHistoryItemOpenEventListener ( this . handleOnAppHistoryItemOpenEventListener ) ;
}
为了防止内存泄漏,不要忘记取消订阅:
componentWillUnmount ( ) {
< ... >
SearchApi . removeOnSpotlightItemOpenEventListener ( this . handleOnSpotlightItemOpenEventListener ) ;
SearchApi . removeOnAppHistoryItemOpenEventListener ( this . handleOnAppHistoryItemOpenEventListener )
}
一般来说,您应该对应用程序是否是使用搜索启动的感兴趣,因此考虑使用以下两种方法:
// For the spotlight item:
SearchApi . getInitialSpotlightItem ( ) . then ( result => {
if ( result ) {
console . log ( 'Started with a spotlight item!' )
}
} )
// For the app history item:
SearchApi . getInitialAppHistoryItem ( ) . then ( result => {
if ( result ) {
console . log ( 'Started with an app history item!' )
}
} )
为了创建新的聚光灯项目,请使用indexSpotlightItem
或indexSpotlightItems
:
SearchApi . indexSpotlightItem ( item ) . then ( result => {
console . log ( 'Success' ) ;
} ) . catch ( err => {
console . log ( 'Error: ' + err ) ;
} ) ;
要将新项目添加到应用程序历史记录中,请使用createUserActivity
:
SearchApi . indexAppHistoryItem ( item ) . then ( result => {
console . log ( 'Success' ) ;
that . setState ( { labelText : 'Success' } ) ;
} ) . catch ( err => {
console . log ( 'Error: ' + err ) ;
that . setState ( { labelText : ( 'Error: ' + err ) } ) ;
} ) ;
下面列出了该项目可能指定的参数:
用于创建聚光灯和应用程序历史记录项目的字典有一些常见的和一些特定的键,这里是所有可能的键的列表。
title
:字符串项目的标题。两种项目类型都是必需的。
contentDescription
:字符串物品的描述。选修的。
keywords
:数组分配给搜索项的关键字数组。选修的。
thumbnail
:字符串|int|对象将在搜索结果中显示的缩略图。与Image
组件中的source
格式相同。选修的。
示例:
var localItem = {
< ... > ,
thumbnail : require ( '/react-native/img/favicon.png' )
} ;
var remoteItem = {
< ... > ,
thumbnail : { uri : 'https://facebook.github.io/react-native/docs/assets/favicon.png' }
} ;
请参阅文档了解更多详细信息。
uniqueIdentifier
:字符串重点项目的唯一标识符,稍后在项目打开事件期间传递。必需的。
domain
:字符串焦点项目的域。选修的。
userInfo
:对象一本字典,稍后在项目开放活动期间传递。必需的。
eligibleForPublicIndexing
:布尔值一个标志,设置为true
时允许将项目添加到公共索引。选修的。
expirationDate
:日期用户活动项目的到期日期。选修的。
webpageURL
:字符串页面的 URL,代表应用程序网站上的相同内容。
© 2017 PresenceKit,作者:Ombori AB