Skip to content

Commit

Permalink
Redirect to external screen for links in ios
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozRatej committed Sep 24, 2024
1 parent 0384f80 commit 8dda83b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:humhub/models/channel_message.dart';
import 'package:humhub/models/hum_hub.dart';
import 'package:humhub/models/manifest.dart';
import 'package:humhub/pages/opener.dart';
import 'package:humhub/util/black_list_rules.dart';
import 'package:humhub/util/connectivity_plugin.dart';
import 'package:humhub/util/const.dart';
import 'package:humhub/util/extensions.dart';
Expand Down Expand Up @@ -144,11 +145,19 @@ class WebViewAppState extends ConsumerState<WebView> {
WebViewGlobalController.ajaxSetHeaders(headers: ref.read(humHubProvider).customHeaders);

//Open in external browser
final url = action.request.url!.origin;
final url = action.request.url!.rawValue;
/// First BLOCK everything that rules out as blocked.
if (BlackListRules.check(url)) {
return NavigationActionPolicy.CANCEL;
}
if (!url.startsWith(_manifest.baseUrl) && action.isForMainFrame) {
_authBrowser.launchUrl(action.request);
return NavigationActionPolicy.CANCEL;
}
if (!action.isForMainFrame) {
await launchUrl(action.request.url!.uriValue, mode: LaunchMode.externalApplication);
return NavigationActionPolicy.CANCEL;
}
// 2nd Append customHeader if url is in app redirect and CANCEL the requests without custom headers
if (Platform.isAndroid ||
action.navigationType == NavigationType.LINK_ACTIVATED ||
Expand Down
9 changes: 5 additions & 4 deletions lib/util/auth_in_app_browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ class AuthInAppBrowser extends InAppBrowser {
AuthInAppBrowser({required this.manifest, required this.concludeAuth}) {
settings = InAppBrowserClassSettings(
browserSettings: InAppBrowserSettings(
hideUrlBar: true,
hideToolbarTop: true,
toolbarTopBackgroundColor: HexColor(manifest.themeColor),
),
hideUrlBar: true,
shouldCloseOnBackButtonPressed: true,
toolbarTopBackgroundColor: HexColor(manifest.themeColor),
toolbarTopTintColor: HexColor(manifest.themeColor),
presentationStyle: ModalPresentationStyle.OVER_FULL_SCREEN),
webViewSettings: InAppWebViewSettings(
javaScriptEnabled: true,
useShouldOverrideUrlLoading: true,
Expand Down
16 changes: 16 additions & 0 deletions lib/util/black_list_rules.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class BlackListRules {
// Define the blacklist of URLs
static const List<String> _blacklist = [
'https://discord.com/',
];

/// Checks if the provided [url] is on the blacklist.
/// Returns true if the URL is blacklisted, false otherwise.
static bool check(String url) {
// Normalize the URL by trimming whitespace and converting to lowercase
final normalizedUrl = url.trim().toLowerCase();

// Check if the normalized URL is in the blacklist
return _blacklist.contains(normalizedUrl);
}
}

0 comments on commit 8dda83b

Please sign in to comment.