AppDelegate.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.SfDataGrid.XForms.iOS.SfDataGridRenderer.Init();
  48. Syncfusion.SfPdfViewer.XForms.iOS.SfPdfDocumentViewRenderer.Init();
  49. new Syncfusion.SfBusyIndicator.XForms.iOS.SfBusyIndicatorRenderer();
  50. Syncfusion.SfPullToRefresh.XForms.iOS.SfPullToRefreshRenderer.Init();
  51. // https://github.com/Baseflow/XF-Material-Library
  52. XF.Material.iOS.Material.Init();
  53. // https://github.com/Redth/ZXing.Net.Mobile/tree/master
  54. ZXing.Net.Mobile.Forms.iOS.Platform.Init();
  55. Xamarin.IQKeyboardManager.SharedManager.Enable = true;
  56. Xamarin.IQKeyboardManager.SharedManager.EnableAutoToolbar = true;
  57. Xamarin.IQKeyboardManager.SharedManager.ShouldResignOnTouchOutside = true;
  58. Xamarin.IQKeyboardManager.SharedManager.ShouldToolbarUsesTextFieldTintColor = true;
  59. Xamarin.IQKeyboardManager.SharedManager.KeyboardDistanceFromTextField = 300f;
  60. if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
  61. {
  62. // Request notification permissions from the user
  63. UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
  64. {
  65. // Handle approval
  66. });
  67. }
  68. UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
  69. if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
  70. {
  71. var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
  72. UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
  73. );
  74. app.RegisterUserNotificationSettings(notificationSettings);
  75. }
  76. // check for a notification
  77. // check for a local notification
  78. if (options?.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey) == true)
  79. {
  80. var localNotification =
  81. options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
  82. if (localNotification != null)
  83. {
  84. UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction,
  85. localNotification.AlertBody, UIAlertControllerStyle.Alert);
  86. okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
  87. Window.RootViewController.PresentViewController(okayAlertController, true, null);
  88. // reset our badge
  89. UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
  90. }
  91. }
  92. LoadApplication(new App());
  93. return base.FinishedLaunching(app, options);
  94. }
  95. public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
  96. {
  97. // show an alert
  98. UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction,
  99. notification.AlertBody, UIAlertControllerStyle.Alert);
  100. okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
  101. UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okayAlertController,
  102. true, null);
  103. // reset our badge
  104. UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
  105. }
  106. public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
  107. {
  108. App.LaunchParameters = url.AbsoluteString.Remove(0, 15);
  109. LoadApplication(new App());
  110. return true;
  111. }
  112. #region Error handling
  113. private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs args)
  114. {
  115. LogUnhandledException("TaskScheduler", args.Exception?.GetType(), args.Exception);
  116. }
  117. private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
  118. {
  119. LogUnhandledException("CurrentDomain", args.ExceptionObject?.GetType(), args.ExceptionObject);
  120. }
  121. private static void LogUnhandledException(String source, Type type, object exceptionobject)
  122. {
  123. if (exceptionobject is Exception exception)
  124. MobileLogging.Log(exception, source);
  125. else
  126. MobileLogging.Log($"{source}: {type?.Name ?? "NULL"} -> {exceptionobject}");
  127. }
  128. #endregion
  129. }
  130. }