Skip to content

Commit

Permalink
content: Open links externally on iOS
Browse files Browse the repository at this point in the history
On iOS we prefer LaunchMode.externalApplication because (for
HTTP URLs) LaunchMode.platformDefault uses SFSafariViewController,
which gives an awkward UX as described here:
  https://chat.zulip.org/#narrow/stream/48-mobile/topic/in-app.20browser/near/1169118

Fixes: zulip#280
  • Loading branch information
rajveermalviya authored and gnprice committed Mar 20, 2024
1 parent 4932db1 commit c4c1a61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/widgets/content.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -895,7 +896,15 @@ void _launchUrl(BuildContext context, String urlString) async {
bool launched = false;
String? errorMessage;
try {
launched = await ZulipBinding.instance.launchUrl(url);
launched = await ZulipBinding.instance.launchUrl(url,
mode: switch (defaultTargetPlatform) {
// On iOS we prefer LaunchMode.externalApplication because (for
// HTTP URLs) LaunchMode.platformDefault uses SFSafariViewController,
// which gives an awkward UX as described here:
// https://chat.zulip.org/#narrow/stream/48-mobile/topic/in-app.20browser/near/1169118
TargetPlatform.iOS => UrlLaunchMode.externalApplication,
_ => UrlLaunchMode.platformDefault,
});
} on PlatformException catch (e) {
errorMessage = e.message;
}
Expand Down
6 changes: 5 additions & 1 deletion test/widgets/content_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:checks/checks.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -318,8 +319,11 @@ void main() {
'<p><a href="https://example/">hello</a></p>');

await tapText(tester, find.text('hello'));

final expectedLaunchMode = defaultTargetPlatform == TargetPlatform.iOS ?
LaunchMode.externalApplication : LaunchMode.platformDefault;
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: Uri.parse('https://example/'), mode: LaunchMode.platformDefault));
.single.equals((url: Uri.parse('https://example/'), mode: expectedLaunchMode));
}, variant: const TargetPlatformVariant({TargetPlatform.android, TargetPlatform.iOS}));

testWidgets('multiple links in paragraph', (tester) async {
Expand Down

0 comments on commit c4c1a61

Please sign in to comment.