AppDelegate.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Threading.Tasks;
  3. using Foundation;
  4. using InABox.Mobile;
  5. using InABox.Mobile.iOS;
  6. using Plugin.LocalNotification.Platforms;
  7. using UIKit;
  8. using UserNotifications;
  9. using Xamarin.Forms;
  10. using PopupManager = InABox.Mobile.iOS.PopupManager;
  11. namespace PRS.Mobile.iOS
  12. {
  13. // The UIApplicationDelegate for the application. This class is responsible for launching the
  14. // User Interface of the application, as well as listening (and optionally responding) to
  15. // application events from iOS.
  16. [Register("AppDelegate")]
  17. public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
  18. {
  19. //
  20. // This method is invoked when the application has loaded and is ready to run. In this
  21. // method you should instantiate the window, load the UI into it and then make the window
  22. // visible.
  23. //
  24. // You have 17 seconds to return from this method, or iOS will terminate your application.
  25. //
  26. public override bool FinishedLaunching(UIApplication app, NSDictionary options)
  27. {
  28. AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
  29. TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
  30. DependencyService.Register<Version_iOS>();
  31. DependencyService.Register<IPopupManager, PopupManager>();
  32. global::Xamarin.Forms.Forms.Init();
  33. // https://learn.microsoft.com/en-au/xamarin/essentials/get-started?tabs=windows%2Cios
  34. //Xamarin.Essentials.Platform.Init( () => provide your own сurrent UIViewController provider);
  35. Syncfusion.SfRangeSlider.XForms.iOS.SfRangeSliderRenderer.Init();
  36. Syncfusion.SfImageEditor.XForms.iOS.SfImageEditorRenderer.Init();
  37. Syncfusion.SfMaps.XForms.iOS.SfMapsRenderer.Init();
  38. Syncfusion.XForms.iOS.MaskedEdit.SfMaskedEditRenderer.Init();
  39. Syncfusion.XForms.iOS.TreeView.SfTreeViewRenderer.Init();
  40. Syncfusion.SfSchedule.XForms.iOS.SfScheduleRenderer.Init();
  41. //new Syncfusion.SfSchedule.XForms.iOS.SfScheduleRenderer();
  42. Syncfusion.XForms.iOS.SignaturePad.SfSignaturePadRenderer.Init();
  43. // This needs to be moved to a JIT location (like the SfPopupLayout stuff)
  44. // 2024-04-05 Exception thrown at startup (CGDataProvider.Create)
  45. Syncfusion.XForms.iOS.TabView.SfTabViewRenderer.Init();
  46. Syncfusion.XForms.Pickers.iOS.SfTimePickerRenderer.Init();
  47. Syncfusion.XForms.iOS.RichTextEditor.SfRichTextEditorRenderer.Init();
  48. Syncfusion.SfDataGrid.XForms.iOS.SfDataGridRenderer.Init();
  49. Syncfusion.SfPdfViewer.XForms.iOS.SfPdfDocumentViewRenderer.Init();
  50. new Syncfusion.SfBusyIndicator.XForms.iOS.SfBusyIndicatorRenderer();
  51. Syncfusion.SfPullToRefresh.XForms.iOS.SfPullToRefreshRenderer.Init();
  52. // https://github.com/Baseflow/XF-Material-Library
  53. XF.Material.iOS.Material.Init();
  54. // https://github.com/Redth/ZXing.Net.Mobile/tree/master
  55. ZXing.Net.Mobile.Forms.iOS.Platform.Init();
  56. Xamarin.IQKeyboardManager.SharedManager.Enable = true;
  57. Xamarin.IQKeyboardManager.SharedManager.EnableAutoToolbar = true;
  58. Xamarin.IQKeyboardManager.SharedManager.ShouldResignOnTouchOutside = true;
  59. Xamarin.IQKeyboardManager.SharedManager.ShouldToolbarUsesTextFieldTintColor = true;
  60. Xamarin.IQKeyboardManager.SharedManager.KeyboardDistanceFromTextField = 300f;
  61. if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
  62. {
  63. // Request notification permissions from the user
  64. UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
  65. {
  66. // Handle approval
  67. });
  68. }
  69. UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
  70. if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
  71. {
  72. var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
  73. UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
  74. );
  75. app.RegisterUserNotificationSettings(notificationSettings);
  76. }
  77. // check for a notification
  78. // check for a local notification
  79. if (options?.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey) == true)
  80. {
  81. var localNotification =
  82. options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
  83. if (localNotification != null)
  84. {
  85. UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction,
  86. localNotification.AlertBody, UIAlertControllerStyle.Alert);
  87. okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
  88. Window.RootViewController.PresentViewController(okayAlertController, true, null);
  89. // reset our badge
  90. UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
  91. }
  92. }
  93. LoadApplication(new App());
  94. return base.FinishedLaunching(app, options);
  95. }
  96. public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
  97. {
  98. // show an alert
  99. UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction,
  100. notification.AlertBody, UIAlertControllerStyle.Alert);
  101. okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
  102. UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okayAlertController,
  103. true, null);
  104. // reset our badge
  105. UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
  106. }
  107. public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
  108. {
  109. App.LaunchParameters = url.AbsoluteString.Remove(0, 15);
  110. LoadApplication(new App());
  111. return true;
  112. }
  113. #region Error handling
  114. private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args)
  115. {
  116. LogUnhandledException("TaskScheduler", args.Exception?.GetType(), args.Exception);
  117. }
  118. private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
  119. {
  120. LogUnhandledException("CurrentDomain", args.ExceptionObject?.GetType(), args.ExceptionObject);
  121. }
  122. private static void LogUnhandledException(String source, Type type, object exceptionobject)
  123. {
  124. if (exceptionobject is Exception exception)
  125. MobileLogging.Log(exception, source);
  126. else
  127. MobileLogging.Log($"{source}: {type?.Name ?? "NULL"} -> {exceptionobject}");
  128. }
  129. #endregion
  130. }
  131. }