AppDelegate.swift 3.94 KB
//
//  AppDelegate.swift
//  Runner
//
//  Created by 权海 on 2026/6/11.
//

import Foundation
import UIKit
import Flutter
import UserNotifications
import AppTrackingTransparency
import AdSupport

@Observable
class AppDelegate: NSObject, UIApplicationDelegate {
    
    let flutterEngine: FlutterEngine = FlutterEngine(name: "main_flutter_engine")

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        UserAgent.share.genUA()
        
        UIApplication.shared.registerForRemoteNotifications()
        UNUserNotificationCenter.current().setBadgeCount(0)
        UNUserNotificationCenter.current().delegate = self
        requestIDFAAuthorization()

        flutterEngine.run()
        GeneratedPluginRegistrant.register(with: flutterEngine)
        NativePigeonRegistrar.register(binaryMessenger: flutterEngine.binaryMessenger)
        WatchConnectivityService.shared.activate()
        HealthKitService.shared.startBackgroundObserversIfNeeded()
//        
        return true
    }
    
    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
        let token = tokenParts.joined()

        print("✅ Device Token: \(token)")
    }

    func application(_ application: UIApplication,
                     didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("❌ Failed to register: \(error.localizedDescription)")
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate{
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.banner, .sound, .badge])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        
        // 从通知中获取 URL
        if let payload = userInfo["payload"] as? String,
           let data = payload.data(using: .utf8),
           let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
           let urlString = dict["url"] as? String,
           let url = URL(string: urlString) {
            
            // 通过 openURL 触发 onOpenURL 回调
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
            }
        }
        completionHandler()
    }
}

extension AppDelegate {
    private func requestIDFAAuthorization() {
        if ATTrackingManager.trackingAuthorizationStatus == .notDetermined {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                ATTrackingManager.requestTrackingAuthorization { status in
                    switch status {
                    case .authorized:
                        let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                        print("✅ IDFA: \(idfa)")
                    case .denied:
                        print("❌ 用户拒绝了IDFA授权")
                    case .restricted:
                        print("⚠️ IDFA受限制,无法获取")
                    case .notDetermined:
                        print("⏳ 用户尚未做出选择")
                    @unknown default:
                        print("未知的IDFA授权状态")
                    }
//                    ReyunUtil.initSDK()
                }
            }
        } else {
//            ReyunUtil.initSDK()
        }
    }
}