AppDelegate.cs 6.7 KB

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