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