Skip to content

Commit

Permalink
Fix getInitialSearchItem where it is null when opening the app from a…
Browse files Browse the repository at this point in the history
… cold start state (not backgrounded) (#58)
  • Loading branch information
jedashford authored Mar 18, 2023
1 parent d6061cc commit 467b4a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

\.DS_Store

plugin/build
plugin/build
.idea
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NativeEventSubscription } from 'react-native'

type SpotlightItem = {
title: string
contentDescription?: string
Expand All @@ -18,6 +20,6 @@ export function deleteItemsInDomains(itemDomains: string[]): Promise<void>;

export function deleteAllItems(): Promise<void>

export function searchItemTapped(callback: (uniqueIdentifier: string) => void): void;
export function searchItemTapped(callback: (uniqueIdentifier: string) => void): NativeEventSubscription;

export function getInitialSearchItem(): Promise<string>
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSObject> continueUserActivityObserver;
@property (nonatomic, strong) id<NSObject> bundleDidLoadObserver;
@property (nonatomic, copy) NSString *initialIdentifier;
@property (nonatomic, assign) BOOL hasListeners;

@end

@implementation RCTSpotlightSearch
static NSString *initialIdentifier;

RCT_EXPORT_MODULE();

Expand Down Expand Up @@ -112,7 +113,7 @@ - (void)handleContinueUserActivity:(NSUserActivity *)userActivity {
return;
}

self.initialIdentifier = uniqueItemIdentifier;
initialIdentifier = uniqueItemIdentifier;

if (!self.hasListeners) {
return;
Expand All @@ -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) {
Expand Down

0 comments on commit 467b4a2

Please sign in to comment.