ارفع راسك فوق انته سوري حر ... :green_heart::green_heart:

Skip to content
Snippets Groups Projects
Select Git revision
  • leaderBranch
  • BMVersion
  • main default protected
3 results

AppDelegate.swift

Blame
  • AppDelegate.swift 7.32 KiB
    //
    // Copyright 2013 - 2021 Anton Tananaev (anton@traccar.org)
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    //     http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    //
    
    import UIKit
    import CoreData
    import Firebase
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, PositionProviderDelegate {
        
        var window: UIWindow?
        
        var managedObjectContext: NSManagedObjectContext?
        var managedObjectModel: NSManagedObjectModel?
        var persistentStoreCoordinator: NSPersistentStoreCoordinator?
        
        var trackingController: TrackingController?
        var positionProvider: PositionProvider?
        
        static var instance: AppDelegate {
            return UIApplication.shared.delegate as! AppDelegate
        }
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
            #if FIREBASE
            FirebaseApp.configure()
            #endif
    
            UIDevice.current.isBatteryMonitoringEnabled = true
    
            let userDefaults = UserDefaults.standard
            if userDefaults.string(forKey: "device_id_preference") == nil {
                let identifier = "\(Int.random(in: 100000..<1000000))"
                userDefaults.setValue(identifier, forKey: "device_id_preference")
            }
    
            registerDefaultsFromSettingsBundle()
            
            migrateLegacyDefaults()
            
            let modelUrl = Bundle.main.url(forResource: "TraccarClient", withExtension: "momd")
            managedObjectModel = NSManagedObjectModel(contentsOf: modelUrl!)
            
            persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel!)
            let storeUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last?.appendingPathComponent("TraccarClient.sqlite")
            let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
            try! persistentStoreCoordinator?.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeUrl, options: options)
            
            managedObjectContext = NSManagedObjectContext.init(concurrencyType: .mainQueueConcurrencyType)
            managedObjectContext?.persistentStoreCoordinator = persistentStoreCoordinator
    
            if userDefaults.bool(forKey: "service_status_preference") {
                StatusViewController.addMessage(NSLocalizedString("Service created", comment: ""))
                trackingController = TrackingController()
                trackingController?.start()
            }