Newer
Older
Productization_H5_IOS / template / Entrance / AppDelegate.swift
@zhangfeng zhangfeng on 8 Aug 2023 1 KB commit first
//
//  AppDelegate.swift
//  template
//
//  Created by 张丰 on 2023/7/22.
//

import UIKit
import UserNotifications
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow.init(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = .white
        self.window?.rootViewController = SYWebVC.init(HOME_H5_URL)
        self.window?.makeKeyAndVisible()
        self.setupAppStatus(isBackground: "0")
        self.regisPush()
        return true
    }

    func regisPush() {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert,.badge,.sound]) { granted, error in
            
        }
        UIApplication.shared.registerForRemoteNotifications()
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        RCIMClient.shared().setDeviceTokenData(deviceToken)
    }
    
    func setupAppStatus(isBackground:String) {
        //此处SuiteName要与创建App Groups勾选的一致
        let shareDefaults = UserDefaults.init(suiteName: "group.com.xf.template")
        shareDefaults?.set(isBackground, forKey: "isBackground")
        shareDefaults?.synchronize()
        debugPrint("当前状态:%@",isBackground)
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        self.setupAppStatus(isBackground: "0")
    }
    func applicationDidEnterBackground(_ application: UIApplication) {
        self.setupAppStatus(isBackground: "1")
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        self.setupAppStatus(isBackground: "0")
    }

    func applicationWillTerminate(_ application: UIApplication) {
        self.setupAppStatus(isBackground: "0")
    }
}