AppDelegate.cs 7.1 KB

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