From 467b4a21c4f717eebc48080f417f3c3698c88b10 Mon Sep 17 00:00:00 2001 From: Jed Ashford Date: Sat, 18 Mar 2023 02:50:31 -0600 Subject: [PATCH] Fix getInitialSearchItem where it is null when opening the app from a cold start state (not backgrounded) (#58) --- .gitignore | 3 ++- README.md | 11 +++++++++++ index.d.ts | 4 +++- .../RCTSpotlightSearch/RCTSpotlightSearch.m | 7 ++++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 10b55b7..e23da66 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ \.DS_Store -plugin/build \ No newline at end of file +plugin/build +.idea \ No newline at end of file diff --git a/README.md b/README.md index e826bd0..6c942a9 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,17 @@ Optionally, if you want to capture the search item that was tapped to open the a SpotlightSearch.getInitialSearchItem().then((uniqueIdentifier) => { alert(`You tapped on ${uniqueIdentifier} and opened the app!`); }); + +// example in a useEffect with listener cleanup +useEffect(() => { + const spotlightListener = SpotlightSearch.searchItemTapped((uniqueIdentifier) => { + alert(`You tapped on ${uniqueIdentifier} and opened the app!`); + }) + return () => { + // cleanup listener + spotlightListener.remove() + } +}, []) ``` The parameter will be the `uniqueIdentifier` that the item was indexed with. You can use this to lookup the item and display information about it, e.g. by navigating to a relevant page in your app. diff --git a/index.d.ts b/index.d.ts index 27ddf97..db9ffe5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,5 @@ +import { NativeEventSubscription } from 'react-native' + type SpotlightItem = { title: string contentDescription?: string @@ -18,6 +20,6 @@ export function deleteItemsInDomains(itemDomains: string[]): Promise; export function deleteAllItems(): Promise -export function searchItemTapped(callback: (uniqueIdentifier: string) => void): void; +export function searchItemTapped(callback: (uniqueIdentifier: string) => void): NativeEventSubscription; export function getInitialSearchItem(): Promise \ No newline at end of file diff --git a/ios/RCTSpotlightSearch/RCTSpotlightSearch/RCTSpotlightSearch.m b/ios/RCTSpotlightSearch/RCTSpotlightSearch/RCTSpotlightSearch.m index a04d7e2..a6cecab 100644 --- a/ios/RCTSpotlightSearch/RCTSpotlightSearch/RCTSpotlightSearch.m +++ b/ios/RCTSpotlightSearch/RCTSpotlightSearch/RCTSpotlightSearch.m @@ -15,17 +15,18 @@ static NSString *const kHandleContinueUserActivityNotification = @"handleContinueUserActivity"; static NSString *const kUserActivityKey = @"userActivity"; static NSString *const kSpotlightSearchItemTapped = @"spotlightSearchItemTapped"; +static NSString * initialIdentifier = @""; @interface RCTSpotlightSearch () @property (nonatomic, strong) id continueUserActivityObserver; @property (nonatomic, strong) id bundleDidLoadObserver; -@property (nonatomic, copy) NSString *initialIdentifier; @property (nonatomic, assign) BOOL hasListeners; @end @implementation RCTSpotlightSearch +static NSString *initialIdentifier; RCT_EXPORT_MODULE(); @@ -112,7 +113,7 @@ - (void)handleContinueUserActivity:(NSUserActivity *)userActivity { return; } - self.initialIdentifier = uniqueItemIdentifier; + initialIdentifier = uniqueItemIdentifier; if (!self.hasListeners) { return; @@ -122,7 +123,7 @@ - (void)handleContinueUserActivity:(NSUserActivity *)userActivity { } RCT_EXPORT_METHOD(getInitialSearchItem:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - resolve(self.initialIdentifier); + resolve(initialIdentifier); } RCT_EXPORT_METHOD(indexItem:(NSDictionary *)item resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {