AppDelegate.cs 6.9 KB

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