AppDelegate.cs 7.5 KB

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