โมดูล SearchApi
ให้อินเทอร์เฟซ React Native ทั่วไปแก่คุณเพื่อโต้ตอบกับ iOS Search API, Core Spotlight
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ 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 )
}
โดยทั่วไป คุณควรสนใจว่าแอปนั้นเริ่มใช้การค้นหาหรือไม่ ดังนั้นให้พิจารณาใช้สองวิธีต่อไปนี้:
// 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 ภาพขนาดย่อที่จะนำเสนอในผลการค้นหา รูปแบบเดียวกับ source
ในคอมโพเนนต์ Image
ไม่จำเป็น.
ตัวอย่าง:
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
: stringโดเมนสำหรับรายการสปอตไลท์ ไม่จำเป็น.
userInfo
: วัตถุพจนานุกรมที่ถูกส่งต่อในช่วงเปิดรายการ ที่จำเป็น.
eligibleForPublicIndexing
: boolean ธงที่เมื่อตั้งค่าเป็น true
จะอนุญาตให้เพิ่มรายการลงในดัชนีสาธารณะ ไม่จำเป็น.
expirationDate
: วันที่วันหมดอายุของรายการกิจกรรมผู้ใช้ ไม่จำเป็น.
webpageURL
: stringURL ของหน้าซึ่งแสดงเนื้อหาเดียวกันบนเว็บไซต์ของแอป
© 2017 PresenceKit โดย Ombori AB