AppDelegate.cs 6.7 KB

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