AppDelegate.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. new Syncfusion.SfSchedule.XForms.iOS.SfScheduleRenderer();
  48. XF.Material.iOS.Material.Init();
  49. ZXing.Net.Mobile.Forms.iOS.Platform.Init();
  50. Xamarin.IQKeyboardManager.SharedManager.Enable = true;
  51. Syncfusion.XForms.iOS.SignaturePad.SfSignaturePadRenderer.Init();
  52. Syncfusion.XForms.iOS.TabView.SfTabViewRenderer.Init();
  53. if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
  54. {
  55. // Request notification permissions from the user
  56. UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
  57. {
  58. // Handle approval
  59. });
  60. }
  61. UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
  62. if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
  63. {
  64. var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
  65. UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
  66. );
  67. uiApplication.RegisterUserNotificationSettings(notificationSettings);
  68. }
  69. // check for a notification
  70. if (launchOptions != null)
  71. {
  72. // check for a local notification
  73. if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
  74. {
  75. var localNotification =
  76. launchOptions[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. }
  88. LoadApplication(new App());
  89. return base.FinishedLaunching(uiApplication, launchOptions);
  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. GlobalVariables.LoadFromLinkString = url.AbsoluteString.Remove(0, 17);
  105. LoadApplication(new App());
  106. return true;
  107. }
  108. #region Error handling
  109. private static void TaskSchedulerOnUnobservedTaskException(object sender,
  110. UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
  111. {
  112. var newExc = new Exception("TaskSchedulerOnUnobservedTaskException",
  113. unobservedTaskExceptionEventArgs.Exception);
  114. LogUnhandledException(newExc);
  115. }
  116. private static void CurrentDomainOnUnhandledException(object sender,
  117. UnhandledExceptionEventArgs unhandledExceptionEventArgs)
  118. {
  119. var newExc = new Exception("CurrentDomainOnUnhandledException",
  120. unhandledExceptionEventArgs.ExceptionObject as Exception);
  121. LogUnhandledException(newExc);
  122. }
  123. internal static void LogUnhandledException(Exception exception)
  124. {
  125. try
  126. {
  127. const string errorFileName = "Fatal.log";
  128. var libraryPath =
  129. Environment.GetFolderPath(Environment.SpecialFolder
  130. .Personal); // iOS: Environment.SpecialFolder.Resources
  131. var errorFilePath = Path.Combine(libraryPath, errorFileName);
  132. var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
  133. DateTime.Now, exception.ToString());
  134. File.WriteAllText(errorFilePath, errorMessage);
  135. }
  136. catch
  137. {
  138. // just suppress any error logging exceptions
  139. }
  140. }
  141. [Conditional("DEBUG")]
  142. //// <summary>
  143. // If there is an unhandled exception, the exception information is diplayed
  144. // on screen the next time the app is started (only in debug configuration)
  145. /// </summary>
  146. private static void DisplayCrashReport()
  147. {
  148. const string errorFilename = "Fatal.log";
  149. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  150. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  151. if (!File.Exists(errorFilePath))
  152. {
  153. return;
  154. }
  155. var errorText = File.ReadAllText(errorFilePath);
  156. var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear")
  157. { UserInteractionEnabled = true };
  158. alertView.Clicked += (sender, args) =>
  159. {
  160. if (args.ButtonIndex != 0)
  161. {
  162. File.Delete(errorFilePath);
  163. }
  164. };
  165. alertView.Show();
  166. }
  167. #endregion
  168. }
  169. }