Modul SearchApi
memberi Anda antarmuka React Native umum untuk berinteraksi dengan iOS Search API, Core Spotlight.
Untuk informasi lebih lanjut mengenai API Pencarian iOS, lihat https://developer.apple.com/ios/search/.
npm install react-native-search-api --save
react-native link
Di bagian atas AppDelegate.m
Anda tambahkan baris berikut:
# import " RCTSearchApiManager.h "
Dalam implementasi AppDelegate Anda, tambahkan yang berikut ini:
- ( BOOL )application:(UIApplication *)application continueUserActivity:( NSUserActivity *)userActivity restorationHandler:( void (^)( NSArray * _Nullable))restorationHandler {
return [RCTSearchApiManager application: application continueUserActivity: userActivity restorationHandler: restorationHandler];
}
Berlangganan ke item pencarian, buka acara di komponen Anda seperti ini:
componentDidMount ( ) {
< ... >
SearchApi . addOnSpotlightItemOpenEventListener ( this . handleOnSpotlightItemOpenEventListener ) ;
SearchApi . addOnAppHistoryItemOpenEventListener ( this . handleOnAppHistoryItemOpenEventListener ) ;
}
Untuk mencegah kebocoran memori jangan lupa berhenti berlangganan:
componentWillUnmount ( ) {
< ... >
SearchApi . removeOnSpotlightItemOpenEventListener ( this . handleOnSpotlightItemOpenEventListener ) ;
SearchApi . removeOnAppHistoryItemOpenEventListener ( this . handleOnAppHistoryItemOpenEventListener )
}
Umumnya Anda akan tertarik apakah aplikasi tersebut mulai menggunakan pencarian, oleh karena itu pertimbangkan untuk menggunakan dua metode berikut:
// 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!' )
}
} )
Untuk membuat item sorotan baru, gunakan indexSpotlightItem
atau indexSpotlightItems
:
SearchApi . indexSpotlightItem ( item ) . then ( result => {
console . log ( 'Success' ) ;
} ) . catch ( err => {
console . log ( 'Error: ' + err ) ;
} ) ;
Untuk menambahkan item baru ke riwayat aplikasi, gunakan createUserActivity
:
SearchApi . indexAppHistoryItem ( item ) . then ( result => {
console . log ( 'Success' ) ;
that . setState ( { labelText : 'Success' } ) ;
} ) . catch ( err => {
console . log ( 'Error: ' + err ) ;
that . setState ( { labelText : ( 'Error: ' + err ) } ) ;
} ) ;
Parameter yang dapat ditentukan oleh item tercantum di bawah ini:
Kamus, yang diteruskan untuk membuat sorotan dan item riwayat aplikasi memiliki beberapa kunci umum dan beberapa kunci spesifik, berikut adalah daftar semua kunci yang mungkin.
title
: taliJudul barang. Diperlukan untuk kedua jenis item.
contentDescription
: stringDeskripsi barang. Opsional.
keywords
: ArraySerangkaian kata kunci, yang ditetapkan ke item pencarian. Opsional.
thumbnail
: string|int|objek Thumbnail untuk disajikan dalam hasil pencarian. Format yang sama dengan source
di komponen Image
. Opsional.
Contoh:
var localItem = {
< ... > ,
thumbnail : require ( '/react-native/img/favicon.png' )
} ;
var remoteItem = {
< ... > ,
thumbnail : { uri : 'https://facebook.github.io/react-native/docs/assets/favicon.png' }
} ;
Silakan merujuk ke dokumentasi untuk lebih jelasnya.
uniqueIdentifier
: stringPengidentifikasi unik dari item sorotan, diteruskan nanti selama acara pembukaan item. Diperlukan.
domain
: taliDomain untuk item sorotan. Opsional.
userInfo
: ObjekKamus, diteruskan nanti saat acara pembukaan item. Diperlukan.
eligibleForPublicIndexing
: boolean Sebuah tanda, yang bila disetel ke true
memungkinkan untuk menambahkan item ke indeks publik. Opsional.
expirationDate
: TanggalTanggal kedaluwarsa item aktivitas pengguna. Opsional.
webpageURL
: stringURL laman, mewakili konten yang sama di situs web aplikasi.
© 2017 PresenceKit oleh Ombori AB