AppDelegate.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. 
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using Foundation;
  7. using InABox.Mobile.iOS;
  8. using Plugin.LocalNotification.Platforms;
  9. using UIKit;
  10. using UserNotifications;
  11. using Xamarin.Forms;
  12. using Syncfusion.TreeView;
  13. using Syncfusion.XForms.iOS.TreeView;
  14. using Syncfusion.XForms.iOS.PopupLayout;
  15. namespace comal.timesheets.iOS
  16. {
  17. // The UIApplicationDelegate for the application. This class is responsible for launching the
  18. // User Interface of the application, as well as listening (and optionally responding) to
  19. // application events from iOS.
  20. [Register("AppDelegate")]
  21. public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
  22. {
  23. //
  24. // This method is invoked when the application has loaded and is ready to run. In this
  25. // method you should instantiate the window, load the UI into it and then make the window
  26. // visible.
  27. //
  28. // You have 17 seconds to return from this method, or iOS will terminate your application.
  29. //
  30. public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
  31. {
  32. AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
  33. TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
  34. DependencyService.Register<Version_iOS>();
  35. global::Xamarin.Forms.Forms.Init();
  36. DisplayCrashReport();
  37. Syncfusion.XForms.iOS.PopupLayout.SfPopupLayoutRenderer.Init();
  38. Syncfusion.SfPdfViewer.XForms.iOS.SfPdfDocumentViewRenderer.Init();
  39. Syncfusion.SfRangeSlider.XForms.iOS.SfRangeSliderRenderer.Init();
  40. Syncfusion.SfImageEditor.XForms.iOS.SfImageEditorRenderer.Init();
  41. Syncfusion.SfDataGrid.XForms.iOS.SfDataGridRenderer.Init();
  42. Syncfusion.XForms.iOS.RichTextEditor.SfRichTextEditorRenderer.Init();
  43. Syncfusion.SfMaps.XForms.iOS.SfMapsRenderer.Init();
  44. Syncfusion.XForms.iOS.MaskedEdit.SfMaskedEditRenderer.Init();
  45. SfTreeViewRenderer.Init();
  46. Syncfusion.SfSchedule.XForms.iOS.SfScheduleRenderer.Init();
  47. XF.Material.iOS.Material.Init();
  48. ZXing.Net.Mobile.Forms.iOS.Platform.Init();
  49. Xamarin.IQKeyboardManager.SharedManager.Enable = true;
  50. Syncfusion.XForms.iOS.SignaturePad.SfSignaturePadRenderer.Init();
  51. Syncfusion.XForms.iOS.TabView.SfTabViewRenderer.Init();
  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. uiApplication.RegisterUserNotificationSettings(notificationSettings);
  67. }
  68. // check for a notification
  69. if (launchOptions != null)
  70. {
  71. // check for a local notification
  72. if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
  73. {
  74. var localNotification =
  75. launchOptions[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
  76. if (localNotification != null)
  77. {
  78. UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction,
  79. localNotification.AlertBody, UIAlertControllerStyle.Alert);
  80. okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
  81. Window.RootViewController.PresentViewController(okayAlertController, true, null);
  82. // reset our badge
  83. UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
  84. }
  85. }
  86. }
  87. LoadApplication(new App());
  88. return base.FinishedLaunching(uiApplication, launchOptions);
  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. GlobalVariables.LoadFromLinkString = url.AbsoluteString.Remove(0, 17);
  104. LoadApplication(new App());
  105. return true;
  106. }
  107. #region Error handling
  108. private static void TaskSchedulerOnUnobservedTaskException(object sender,
  109. UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
  110. {
  111. var newExc = new Exception("TaskSchedulerOnUnobservedTaskException",
  112. unobservedTaskExceptionEventArgs.Exception);
  113. LogUnhandledException(newExc);
  114. }
  115. private static void CurrentDomainOnUnhandledException(object sender,
  116. UnhandledExceptionEventArgs unhandledExceptionEventArgs)
  117. {
  118. var newExc = new Exception("CurrentDomainOnUnhandledException",
  119. unhandledExceptionEventArgs.ExceptionObject as Exception);
  120. LogUnhandledException(newExc);
  121. }
  122. internal static void LogUnhandledException(Exception exception)
  123. {
  124. try
  125. {
  126. const string errorFileName = "Fatal.log";
  127. var libraryPath =
  128. Environment.GetFolderPath(Environment.SpecialFolder
  129. .Personal); // iOS: Environment.SpecialFolder.Resources
  130. var errorFilePath = Path.Combine(libraryPath, errorFileName);
  131. var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
  132. DateTime.Now, exception.ToString());
  133. File.WriteAllText(errorFilePath, errorMessage);
  134. }
  135. catch
  136. {
  137. // just suppress any error logging exceptions
  138. }
  139. }
  140. [Conditional("DEBUG")]
  141. //// <summary>
  142. // If there is an unhandled exception, the exception information is diplayed
  143. // on screen the next time the app is started (only in debug configuration)
  144. /// </summary>
  145. private static void DisplayCrashReport()
  146. {
  147. const string errorFilename = "Fatal.log";
  148. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  149. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  150. if (!File.Exists(errorFilePath))
  151. {
  152. return;
  153. }
  154. var errorText = File.ReadAllText(errorFilePath);
  155. var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear")
  156. { UserInteractionEnabled = true };
  157. alertView.Clicked += (sender, args) =>
  158. {
  159. if (args.ButtonIndex != 0)
  160. {
  161. File.Delete(errorFilePath);
  162. }
  163. };
  164. alertView.Show();
  165. }
  166. #endregion
  167. }
  168. }