Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.2 KiB

  1. import 'package:android_intent_plus/android_intent.dart';
  2. import 'package:platform/platform.dart';
  3. import 'package:sonnat/core/utils/utilities.dart';
  4. import 'package:url_launcher/url_launcher.dart';
  5. class UrlLauncher {
  6. Future<void> launchInBrowser(String url) async {
  7. try {
  8. await launchUrl(
  9. Uri.parse(url),
  10. mode: LaunchMode.externalApplication,
  11. );
  12. } on Exception catch (_) {
  13. if (Utilities.isAndroid) {
  14. AndroidIntent intent = AndroidIntent(action: 'action_view', package: 'com.habibapp.habib', data: url);
  15. await intent.launch();
  16. }
  17. }
  18. }
  19. Future<void> launchInWebViewOrVC(String url) async {
  20. if (await canLaunchUrl(Uri.parse(url))) {
  21. await launch(
  22. url,
  23. forceSafariVC: true,
  24. forceWebView: true,
  25. headers: <String, String>{'my_header_key': 'my_header_value'},
  26. );
  27. } else {
  28. throw 'Could not launch $url';
  29. }
  30. }
  31. Future<void> launchInWebViewWithJavaScript(String url) async {
  32. if (await canLaunchUrl(Uri.parse(url))) {
  33. await launch(
  34. url,
  35. forceSafariVC: true,
  36. forceWebView: true,
  37. enableJavaScript: true,
  38. );
  39. } else {
  40. throw 'Could not launch $url';
  41. }
  42. }
  43. Future<void> launchInWebViewWithDomStorage(String url) async {
  44. if (await canLaunchUrl(Uri.parse(url))) {
  45. await launch(
  46. url,
  47. forceSafariVC: true,
  48. forceWebView: true,
  49. enableDomStorage: true,
  50. );
  51. } else {
  52. throw 'Could not launch $url';
  53. }
  54. }
  55. Future<void> launchUniversalLinkIos(String url) async {
  56. if (await canLaunchUrl(Uri.parse(url))) {
  57. final bool nativeAppLaunchSucceeded = await launch(
  58. url,
  59. forceSafariVC: false,
  60. universalLinksOnly: true,
  61. );
  62. if (!nativeAppLaunchSucceeded) {
  63. await launchUrl(Uri.parse(url));
  64. }
  65. }
  66. }
  67. Future<void> makePhoneCall(String url) async {
  68. await launchUrl(Uri.parse('tel:$url'));
  69. }
  70. Future<void> textTo(String url, String body) async {
  71. if (const LocalPlatform().isAndroid) {
  72. var uri = 'sms:+$url?$body';
  73. await launchUrl(Uri.parse(uri));
  74. } else if (const LocalPlatform().isIOS) {
  75. var uri = 'sms:00$url?$body';
  76. await launchUrl(Uri.parse(uri));
  77. }
  78. }
  79. }