tryLaunchApp function

Future<bool> tryLaunchApp({
  1. required String deeplink,
})

Implementation

Future<bool> tryLaunchApp({required String deeplink}) async {
  if (kIsWeb) {
    return await launchUrl(Uri.parse(deeplink),
        mode: LaunchMode.externalApplication);
  }

  final navigator = getObject<INavigationHelpers>();
  final canLaunch = await canLaunchUrlString(deeplink);

  final url = Uri.parse(deeplink);
  const forceOpenKey = '_force';

  final shouldForceOpen = url.queryParameters[forceOpenKey];

  final deeplinkUrlWithoutFallback = url
      .replace(
          queryParameters: {
        ...url.queryParameters,
      }..remove(forceOpenKey))
      .toString();

  deeplink = deeplinkUrlWithoutFallback.toString();

  if (!canLaunch) {
    if (shouldForceOpen == 'true') {
      getObject<IHubblePlatformUtilities>().openLink(
        Uri.parse(deeplink),
      );
      return false;
    }

    navigator.snack.error(
      title: 'Uh-Oh',
      message: 'We could not open the app',
    );

    return false;
  }

  final isAppOpenSuccess = await launchUrlString(
    deeplink,
    mode: LaunchMode.externalNonBrowserApplication,
  ).catchError((error) => false);

  if (!isAppOpenSuccess && kDebugMode) {
    navigator.snack.error(
      title: 'Uh-Oh',
      message: 'We could not open the app',
    );
  }

  if (isAppOpenSuccess) {
    return true;
  }

  final isLaunchSuccess = await launchUrlString(
    deeplink,
    mode: PlatformUtils.isIOS
        ? LaunchMode.inAppWebView
        : LaunchMode.externalApplication,
  ).catchError((error) => false);

  if (isLaunchSuccess) {
    return true;
  } else {
    navigator.snack.error(
      title: 'Uh-Oh',
      message: 'We could not open the app',
    );
    return false;
  }
}