patbef-iOS/Befund/AppDelegate.swift

84 lines
3.7 KiB
Swift
Raw Normal View History

2024-01-29 16:20:42 +01:00
//
// AppDelegate.swift
// Befund
//
// Created by Irakli Abetschkhrischwili on 29.04.22.
// Copyright © 2022 MVZ Dr. Stein und Kollegen. All rights reserved.
import UIKit
import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
public static var Session: Core.Models.Session = Core.Models.Session()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
Initialize(application: application)
sleep(1)
return true
}
/**
* Initializes the application contexts
*/
func Initialize(application: UIApplication)
{
Core.Log.Initialize()
Core.Database.Results.CreateDBIfNotExists()
//let pre = Locale.current.language.languageCode?.identifier
//Core.Lang.SetLanguage(lang: (pre != "de" ? Core.Lang.Languages.EN : Core.Lang.Languages.DE))
//Core.Lang.SetLanguage(lang: .DE)
AppDelegate.Session.Device = Core.System.GetDevice()
AppDelegate.Session.NotificationGranted = false
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
AppDelegate.Session.Device!.token = deviceToken.map { data in String(format: "%02.2hhx", data) }.joined()
Core.Log.Info(msg: "Device got a push notification token", namespace: "AppDelegate", method: "application(didRegisterForRemoteNotificationsWithDeviceToken)")
ViewController.CurrentViewController?.onSessionChanged(session: AppDelegate.Session)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
AppDelegate.Session.NotificationGranted = false
let nsError = error as NSError
if (nsError.code==3010)
{
//emulator doesn't support push notification and can't be registered and get device_token.
//But we need it for test Request where device_token is checked for nil/empty
AppDelegate.Session.Device!.token = "EmulatorDeviceToken1234567890"
Core.Log.Info(msg: "Set device token to EmulatorDeviceToken1234567890, because in emulator mode remote notification is not supported. ", namespace: "AppDelegate", method: "application(didFailToRegisterForRemoteNotificationsWithError)")
}
else
{
AppDelegate.Session.Device!.token = nil
Core.Log.Critical(err: error, namespace: "AppDelegate", method: "application(didFailToRegisterForRemoteNotificationsWithError)")
}
if(ViewController.CurrentViewController != nil)
{
ViewController.CurrentViewController!.onSessionChanged(session: AppDelegate.Session)
}
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}