Skip to content

Commit 9a00554

Browse files
fix: deep and universal links when app is running (#140)
Co-authored-by: Thiago Brezinski <[email protected]>
1 parent fef6d8e commit 9a00554

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

packages/react-native/Libraries/LinkingIOS/RCTLinkingManager.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
continueUserActivity:(nonnull NSUserActivity *)userActivity
2525
restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> *_Nullable))restorationHandler;
2626

27+
+ (void)onOpenURL:(nonnull NSURL *)url NS_SWIFT_NAME(onOpenURL(url:));
28+
2729
@end

packages/react-native/Libraries/LinkingIOS/RCTLinkingManager.mm

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "RCTLinkingPlugins.h"
1616

1717
static NSString *const kOpenURLNotification = @"RCTOpenURLNotification";
18+
static NSURL *initialURL = nil;
1819

1920
static void postNotificationWithURL(NSURL *URL, id sender)
2021
{
@@ -81,6 +82,16 @@ + (BOOL)application:(UIApplication *)application
8182
return YES;
8283
}
8384

85+
86+
+ (void)onOpenURL:(NSURL *)url
87+
{
88+
if (initialURL == nil) {
89+
initialURL = url;
90+
} else {
91+
postNotificationWithURL(url, self);
92+
}
93+
}
94+
8495
- (void)handleOpenURLNotification:(NSNotification *)notification
8596
{
8697
[self sendEventWithName:@"url" body:notification.userInfo];
@@ -153,6 +164,7 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
153164

154165
RCT_EXPORT_METHOD(getInitialURL : (RCTPromiseResolveBlock)resolve reject : (__unused RCTPromiseRejectBlock)reject)
155166
{
167+
#if !TARGET_OS_VISION
156168
NSURL *initialURL = nil;
157169
if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
158170
initialURL = self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
@@ -163,6 +175,8 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
163175
initialURL = ((NSUserActivity *)userActivityDictionary[@"UIApplicationLaunchOptionsUserActivityKey"]).webpageURL;
164176
}
165177
}
178+
#endif
179+
// React Native visionOS uses static property to retrieve initialURL.
166180
resolve(RCTNullIfNil(initialURL.absoluteString));
167181
}
168182

packages/react-native/Libraries/SwiftExtensions/RCTMainWindow.swift

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import SwiftUI
2020
public struct RCTMainWindow: Scene {
2121
var moduleName: String
2222
var initialProps: RCTRootViewRepresentable.InitialPropsType
23+
var onOpenURLCallback: ((URL) -> ())?
2324

2425
public init(moduleName: String, initialProps: RCTRootViewRepresentable.InitialPropsType = nil) {
2526
self.moduleName = moduleName
@@ -30,10 +31,21 @@ public struct RCTMainWindow: Scene {
3031
WindowGroup {
3132
RCTRootViewRepresentable(moduleName: moduleName, initialProps: initialProps)
3233
.modifier(WindowHandlingModifier())
34+
.onOpenURL(perform: { url in
35+
onOpenURLCallback?(url)
36+
})
3337
}
3438
}
3539
}
3640

41+
extension RCTMainWindow {
42+
public func onOpenURL(perform action: @escaping (URL) -> ()) -> some Scene {
43+
var scene = self
44+
scene.onOpenURLCallback = action
45+
return scene
46+
}
47+
}
48+
3749
/**
3850
Handles data sharing between React Native and SwiftUI views.
3951
*/

packages/rn-tester/RNTester-visionOS/App.swift

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ struct RNTesterApp: App {
1111

1212
var body: some Scene {
1313
RCTMainWindow(moduleName: "RNTesterApp")
14+
.onOpenURL(perform: { url in
15+
RCTLinkingManager.onOpenURL(url: url)
16+
})
1417

1518
RCTWindow(id: "SecondWindow", sceneData: reactContext.getSceneData(id: "SecondWindow"))
1619
.defaultSize(CGSize(width: 400, height: 700))

packages/rn-tester/RNTester-visionOS/Info.plist

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>CFBundleURLTypes</key>
6+
<array>
7+
<dict>
8+
<key>CFBundleTypeRole</key>
9+
<string>Editor</string>
10+
<key>CFBundleURLName</key>
11+
<string>com.reactjs.ios</string>
12+
<key>CFBundleURLSchemes</key>
13+
<array>
14+
<string>rntester</string>
15+
</array>
16+
</dict>
17+
</array>
518
<key>UIApplicationSceneManifest</key>
619
<dict>
720
<key>UIApplicationPreferredDefaultSceneSessionRole</key>

0 commit comments

Comments
 (0)