@iternio/react-native-auto-play 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -140,6 +140,41 @@ Paste this into your Info.plist and adjust it to your needs. Check [Apple docs](
140
140
  </dict>
141
141
  ```
142
142
 
143
+ #### Deep links
144
+ `WindowApplicationSceneDelegate` forwards custom URL schemes and universal links to React Native's `Linking` API, so no extra `AppDelegate` or scene wiring is needed.
145
+
146
+ - **App not running:** the link is returned by `Linking.getInitialURL()`.
147
+ - **App already running:** the link is delivered as a `Linking` `'url'` event. This includes the case where CarPlay launched the app and the phone UI is opened by a link afterwards.
148
+ - **Links that arrive before JS has called `getInitialURL()`** are kept and returned by that first call. Only the first call returns the link; later calls resolve to `null`.
149
+
150
+ Call `getInitialURL()` once, as early as possible, and before you subscribe to `'url'` events. If you subscribe first and call `getInitialURL()` later, a link that arrives in between is delivered by both, and your app handles it twice. Do not defer the call until a splash screen or sign-in has finished. Read the URL right away and keep it until your app is ready to act on it.
151
+
152
+ ```tsx
153
+ import { useEffect } from 'react';
154
+ import { Linking } from 'react-native';
155
+
156
+ function handleLink(url: string) {
157
+ // navigate, or store the URL until the app is ready
158
+ }
159
+
160
+ export function App() {
161
+ useEffect(() => {
162
+ // Read the initial URL first, then subscribe, in the same effect.
163
+ Linking.getInitialURL().then((url) => {
164
+ if (url) {
165
+ handleLink(url);
166
+ }
167
+ });
168
+ const subscription = Linking.addEventListener('url', ({ url }) => handleLink(url));
169
+ return () => subscription.remove();
170
+ }, []);
171
+
172
+ // ...
173
+ }
174
+ ```
175
+
176
+ React Navigation's `linking` prop already follows this order, so it needs no extra setup.
177
+
143
178
  #### MapTemplate
144
179
  if you want to make use of the MapTemplate and render react components you need to add this to your AppDelegate.swift
145
180
  This is an example that works for bare react-native (>= 0.82) and Expo SDK 57, check [this](https://github.com/Iternio-Planning-AB/react-native-auto-play/blob/dbd33ff32ee58338282ffe0f8a970e687e1e3520/packages/react-native-autoplay/README.md?plain=1#L139) for older versions.
@@ -9,7 +9,8 @@
9
9
 
10
10
  @interface NitroLinkingManager : NSObject
11
11
 
12
- @property (nonatomic, strong) NSURL *launchURL;
12
+ @property (atomic, strong) NSURL *launchURL;
13
+ @property (atomic, assign) BOOL initialURLRequested;
13
14
 
14
15
  + (instancetype)shared;
15
16
  - (void)continueUserActivity:(NSUserActivity *)userActivity;
@@ -19,7 +19,12 @@
19
19
  return sharedInstance;
20
20
  }
21
21
 
22
+ // Before JS has asked for the initial URL nothing observes RCTLinkingManager's event, so keep the URL for getInitialURL.
22
23
  - (void)continueUserActivity:(NSUserActivity *)userActivity {
24
+ if (!self.initialURLRequested && [userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]
25
+ && userActivity.webpageURL != nil) {
26
+ self.launchURL = userActivity.webpageURL;
27
+ }
23
28
  [RCTLinkingManager application:UIApplication.sharedApplication
24
29
  continueUserActivity:userActivity
25
30
  restorationHandler:^(NSArray *_Nullable _){
@@ -28,6 +33,9 @@
28
33
 
29
34
  - (void)openURL:(UIOpenURLContext *)urlContext {
30
35
  NSURL *url = urlContext.URL;
36
+ if (!self.initialURLRequested && url != nil) {
37
+ self.launchURL = url;
38
+ }
31
39
  NSDictionary<UIApplicationOpenURLOptionsKey, id> *options = @{
32
40
  UIApplicationOpenURLOptionsSourceApplicationKey :
33
41
  urlContext.options.sourceApplication
@@ -15,7 +15,9 @@
15
15
 
16
16
  RCT_EXPORT_METHOD(getInitialURL:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
17
17
  {
18
+ [NitroLinkingManager shared].initialURLRequested = YES;
18
19
  NSURL *initialURL = [NitroLinkingManager shared].launchURL;
20
+ [NitroLinkingManager shared].launchURL = nil;
19
21
 
20
22
  if (initialURL) {
21
23
  resolve(RCTNullIfNil(initialURL.absoluteString));
@@ -31,9 +31,9 @@ class WindowApplicationSceneDelegate: UIResponder, UIWindowSceneDelegate {
31
31
 
32
32
  self.window = window
33
33
 
34
- if let url = connectionOptions.urlContexts.first?.url {
34
+ if let urlContext = connectionOptions.urlContexts.first {
35
35
  // Linking API -> on app start
36
- NitroLinkingManager.shared().launchURL = url
36
+ NitroLinkingManager.shared().openURL(urlContext)
37
37
  }
38
38
 
39
39
  if let userActivity = connectionOptions.userActivities.first(where: {
@@ -41,7 +41,7 @@ class WindowApplicationSceneDelegate: UIResponder, UIWindowSceneDelegate {
41
41
  userActivity.webpageURL != nil
42
42
  }) {
43
43
  // Universal Links -> on app start
44
- NitroLinkingManager.shared().launchURL = userActivity.webpageURL
44
+ NitroLinkingManager.shared().continue(userActivity)
45
45
  }
46
46
  }
47
47
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iternio/react-native-auto-play",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Android Auto and Apple CarPlay for react-native",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",