AppDelegate.swift
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// AppDelegate.swift
// Runner
//
// Created by 权海 on 2026/6/11.
//
import Foundation
import UIKit
import Flutter
import UserNotifications
import AppTrackingTransparency
import AdSupport
typealias FlutterBridgeMethod = ((_ params: Any?, _ result: FlutterResult) -> Void)
@Observable
class AppDelegate: NSObject, UIApplicationDelegate {
enum FlutterBridgeMethodName: String{
case verifyPayment
}
private var methods: [String: FlutterBridgeMethod] = [:]
let flutterEngine: FlutterEngine = FlutterEngine(name: "main_flutter_engine")
var channel: FlutterMethodChannel?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
AppShared.shared.agent.genUA()
AppShared.shared.payment.delegate = self
UIApplication.shared.registerForRemoteNotifications()
UNUserNotificationCenter.current().setBadgeCount(0)
UNUserNotificationCenter.current().delegate = self
requestIDFAAuthorization()
flutterEngine.run()
GeneratedPluginRegistrant.register(with: flutterEngine)
NativePigeonRegistrar.register(binaryMessenger: flutterEngine.binaryMessenger)
channel = FlutterMethodChannel(name: "doublefeel_flutter_main_channel", binaryMessenger: flutterEngine.binaryMessenger)
methods = defaultMethods()
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{
private func defaultMethods() -> [String: FlutterBridgeMethod]{
var methods: [String: FlutterBridgeMethod] = [:]
methods[FlutterBridgeMethodName.verifyPayment.rawValue] = { params, result in
result(params)
}
return methods
}
func invoke(method: FlutterBridgeMethodName, arguments: Any?, result: @escaping FlutterResult){
channel?.invokeMethod(method.rawValue, arguments: arguments, result: result)
}
}
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()
}
}
}