Skip to content

Commit f6d405c

Browse files
authored
Remove manual Safari fallback for unhandled universal links (flutter#185430)
When a universal link (https) opens an iOS app on a cold start, the Dart framework may not be ready to handle routes yet — onGenerateRoute can return null simply because initialization hasn't completed. The engine interprets this as "unhandled" and relays the URL back to iOS via openURL:, which opens Safari. This is incorrect and disruptive. This PR completely removes the relay-to-system fallback for unhandled universal links (for both warm and cold starts). The modern iOS API for scenes (`UISceneDelegate continueUserActivity`) returns `void`, meaning iOS natively no longer expects feedback on whether a link was handled, and natively stopped bouncing unrecognized universal links to Safari. Removing our manual `[UIApplication openURL:]` fallback aligns Flutter's engine with Apple's modern native behavior. **PR with the help of Gemini** Fixes flutter#170665 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent da7fe06 commit f6d405c

3 files changed

Lines changed: 16 additions & 30 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,12 @@ - (BOOL)application:(UIApplication*)application
149149
return YES;
150150
}
151151

152-
// Relaying to the system here will case an infinite loop, so we don't do it here.
153-
return [self handleOpenURL:url options:options relayToSystemIfUnhandled:NO];
152+
return [self handleOpenURL:url options:options];
154153
}
155154

156155
// Helper function for opening an URL, either with a custom scheme or a http/https scheme.
157156
- (BOOL)handleOpenURL:(NSURL*)url
158-
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options
159-
relayToSystemIfUnhandled:(BOOL)throwBack {
157+
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
160158
UIApplication* flutterApplication = FlutterSharedApplication.application;
161159
if (flutterApplication == nil) {
162160
return NO;
@@ -168,13 +166,8 @@ - (BOOL)handleOpenURL:(NSURL*)url
168166
FlutterViewController* flutterViewController = [self rootFlutterViewController];
169167
if (flutterViewController) {
170168
[flutterViewController.engine sendDeepLinkToFramework:url
171-
completionHandler:^(BOOL success) {
172-
if (!success && throwBack) {
173-
// throw it back to iOS
174-
[flutterApplication openURL:url
175-
options:@{}
176-
completionHandler:nil];
177-
}
169+
completionHandler:^(BOOL success){
170+
// no-op.
178171
}];
179172
} else {
180173
[FlutterLogger logError:@"Attempting to open an URL without a Flutter RootViewController."];
@@ -224,7 +217,7 @@ - (BOOL)application:(UIApplication*)application
224217
return YES;
225218
}
226219

227-
return [self handleOpenURL:userActivity.webpageURL options:@{} relayToSystemIfUnhandled:YES];
220+
return [self handleOpenURL:userActivity.webpageURL options:@{}];
228221
}
229222

230223
#pragma mark - FlutterPluginRegistry methods. All delegating to the rootViewController

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ - (void)testUniversalLinkPushRouteInformation {
192192
OCMVerifyAll(self.mockNavigationChannel);
193193
}
194194

195-
- (void)testUseNonDeprecatedOpenURLAPI {
195+
- (void)testUniversalLinkDoesNotRelayToSystemWhenUnhandled {
196196
OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
197197
.andReturn(@YES);
198198
NSUserActivity* userActivity = [[NSUserActivity alloc] initWithActivityType:@"com.example.test"];
@@ -205,13 +205,15 @@ - (void)testUseNonDeprecatedOpenURLAPI {
205205
});
206206
id mockApplication = OCMClassMock([UIApplication class]);
207207
OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
208+
208209
BOOL result = [self.appDelegate
209210
application:[UIApplication sharedApplication]
210211
continueUserActivity:userActivity
211212
restorationHandler:^(NSArray<id<UIUserActivityRestoring>>* __nullable restorableObjects){
212213
}];
213214
XCTAssertTrue(result);
214-
OCMVerify([mockApplication openURL:[OCMArg any]
215+
// openURL should NOT have been called.
216+
OCMReject([mockApplication openURL:[OCMArg any]
215217
options:[OCMArg any]
216218
completionHandler:[OCMArg any]]);
217219
}

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSceneLifeCycle.mm‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ - (void)scene:(UIScene*)scene openURLContexts:(NSSet<UIOpenURLContext*>*)URLCont
311311
continue;
312312
}
313313
for (UIOpenURLContext* urlContext in URLContexts) {
314-
if ([self handleDeeplink:urlContext.URL flutterEngine:engine relayToSystemIfUnhandled:NO]) {
314+
if ([self handleDeeplink:urlContext.URL flutterEngine:engine]) {
315315
break;
316316
}
317317
}
@@ -344,7 +344,7 @@ - (void)scene:(UIScene*)scene continueUserActivity:(NSUserActivity*)userActivity
344344
if ([enginesHandledByPlugin containsObject:engine]) {
345345
continue;
346346
}
347-
[self handleDeeplink:userActivity.webpageURL flutterEngine:engine relayToSystemIfUnhandled:YES];
347+
[self handleDeeplink:userActivity.webpageURL flutterEngine:engine];
348348
}
349349
}
350350

@@ -436,9 +436,7 @@ - (void)handleDeeplinkingForEngine:(FlutterEngine*)engine
436436
// scene(_:continue:) when the universal link is tapped while your app is running or suspended in
437437
// memory.
438438
for (NSUserActivity* userActivity in connectionOptions.userActivities) {
439-
if ([self handleDeeplink:userActivity.webpageURL
440-
flutterEngine:engine
441-
relayToSystemIfUnhandled:YES]) {
439+
if ([self handleDeeplink:userActivity.webpageURL flutterEngine:engine]) {
442440
return;
443441
}
444442
}
@@ -447,15 +445,13 @@ - (void)handleDeeplinkingForEngine:(FlutterEngine*)engine
447445
// the scene:willConnectToSession:options: delegate method after launch, and to
448446
// scene:openURLContexts: when your app opens a URL while running or suspended in memory.
449447
for (UIOpenURLContext* urlContext in connectionOptions.URLContexts) {
450-
if ([self handleDeeplink:urlContext.URL flutterEngine:engine relayToSystemIfUnhandled:YES]) {
448+
if ([self handleDeeplink:urlContext.URL flutterEngine:engine]) {
451449
return;
452450
}
453451
}
454452
}
455453

456-
- (BOOL)handleDeeplink:(NSURL*)url
457-
flutterEngine:(FlutterEngine*)engine
458-
relayToSystemIfUnhandled:(BOOL)throwBack {
454+
- (BOOL)handleDeeplink:(NSURL*)url flutterEngine:(FlutterEngine*)engine {
459455
if (!url) {
460456
return NO;
461457
}
@@ -465,13 +461,8 @@ - (BOOL)handleDeeplink:(NSURL*)url
465461
}
466462
// if deep linking is enabled, send it to the framework
467463
[engine sendDeepLinkToFramework:url
468-
completionHandler:^(BOOL success) {
469-
if (!success && throwBack) {
470-
// throw it back to iOS
471-
[FlutterSharedApplication.application openURL:url
472-
options:@{}
473-
completionHandler:nil];
474-
}
464+
completionHandler:^(BOOL success){
465+
// no-op.
475466
}];
476467
return YES;
477468
}

0 commit comments

Comments
 (0)