SearchApi
モジュールは、iOS Search API、Core Spotlight と対話するための一般的な React Native インターフェイスを提供します。
iOS Search 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 )
}
一般に、アプリが検索を使用して開始されたかどうかに関心があるはずなので、次の 2 つの方法の使用を検討してください。
// 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
: string|int|object検索結果に表示されるサムネイル。 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
: 文字列アプリの Web サイト上の同じコンテンツを表すページの URL。
© 2017 PresenceKit by オンボリ AB