using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Foundation; using InABox.Mobile.iOS; using Plugin.LocalNotification.Platforms; using UIKit; using UserNotifications; using Xamarin.Forms; using Syncfusion.TreeView; using Syncfusion.XForms.iOS.TreeView; using Syncfusion.XForms.iOS.PopupLayout; namespace comal.timesheets.iOS { // The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { // // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions) { AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; DependencyService.Register(); global::Xamarin.Forms.Forms.Init(); DisplayCrashReport(); Syncfusion.XForms.iOS.PopupLayout.SfPopupLayoutRenderer.Init(); Syncfusion.SfPdfViewer.XForms.iOS.SfPdfDocumentViewRenderer.Init(); Syncfusion.SfRangeSlider.XForms.iOS.SfRangeSliderRenderer.Init(); Syncfusion.SfImageEditor.XForms.iOS.SfImageEditorRenderer.Init(); Syncfusion.SfDataGrid.XForms.iOS.SfDataGridRenderer.Init(); Syncfusion.XForms.iOS.RichTextEditor.SfRichTextEditorRenderer.Init(); Syncfusion.SfMaps.XForms.iOS.SfMapsRenderer.Init(); Syncfusion.XForms.iOS.MaskedEdit.SfMaskedEditRenderer.Init(); SfTreeViewRenderer.Init(); Syncfusion.SfSchedule.XForms.iOS.SfScheduleRenderer.Init(); new Syncfusion.SfSchedule.XForms.iOS.SfScheduleRenderer(); XF.Material.iOS.Material.Init(); ZXing.Net.Mobile.Forms.iOS.Platform.Init(); Xamarin.IQKeyboardManager.SharedManager.Enable = true; Syncfusion.XForms.iOS.SignaturePad.SfSignaturePadRenderer.Init(); Syncfusion.XForms.iOS.TabView.SfTabViewRenderer.Init(); if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { // Request notification permissions from the user UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => { // Handle approval }); } UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate(); if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes( UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null ); uiApplication.RegisterUserNotificationSettings(notificationSettings); } // check for a notification if (launchOptions != null) { // check for a local notification if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey)) { var localNotification = launchOptions[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification; if (localNotification != null) { UIAlertController okayAlertController = UIAlertController.Create(localNotification.AlertAction, localNotification.AlertBody, UIAlertControllerStyle.Alert); okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); Window.RootViewController.PresentViewController(okayAlertController, true, null); // reset our badge UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0; } } } LoadApplication(new App()); return base.FinishedLaunching(uiApplication, launchOptions); } public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification) { // show an alert UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert); okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okayAlertController, true, null); // reset our badge UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0; } public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) { GlobalVariables.LoadFromLinkString = url.AbsoluteString.Remove(0, 17); LoadApplication(new App()); return true; } #region Error handling private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs) { var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception); LogUnhandledException(newExc); } private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) { var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception); LogUnhandledException(newExc); } internal static void LogUnhandledException(Exception exception) { try { const string errorFileName = "Fatal.log"; var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder .Personal); // iOS: Environment.SpecialFolder.Resources var errorFilePath = Path.Combine(libraryPath, errorFileName); var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}", DateTime.Now, exception.ToString()); File.WriteAllText(errorFilePath, errorMessage); } catch { // just suppress any error logging exceptions } } [Conditional("DEBUG")] //// // If there is an unhandled exception, the exception information is diplayed // on screen the next time the app is started (only in debug configuration) /// private static void DisplayCrashReport() { const string errorFilename = "Fatal.log"; var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources); var errorFilePath = Path.Combine(libraryPath, errorFilename); if (!File.Exists(errorFilePath)) { return; } var errorText = File.ReadAllText(errorFilePath); var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear") { UserInteractionEnabled = true }; alertView.Clicked += (sender, args) => { if (args.ButtonIndex != 0) { File.Delete(errorFilePath); } }; alertView.Show(); } #endregion } }