AppDelegate.swift
4.7 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
135
136
137
138
139
140
141
142
143
144
145
146
import UIKit
import Flutter
import StoreKit
// Add these two import lines
import TIMPush
import tencent_cloud_chat_push
@main
@objc class AppDelegate: FlutterAppDelegate,TIMPushDelegate {
private var edgePanRecognizer: UIScreenEdgePanGestureRecognizer?
private var methodChannel: FlutterMethodChannel?
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// 获取 root FlutterViewController 实例
guard let controller = window?.rootViewController as? FlutterViewController else {
fatalError("rootViewController is not a FlutterViewController")
}
let channel = FlutterMethodChannel(
name: "cn.banxe.appframe/receipt",
binaryMessenger: controller.binaryMessenger
)
channel.setMethodCallHandler { (call, result) in
switch call.method {
case "getReceipt":
self.getReceiptBase64(result: result)
case "refreshReceipt":
self.refreshReceipt(result: result)
default:
result(FlutterMethodNotImplemented)
}
}
// 注册插件
GeneratedPluginRegistrant.register(with: self)
// 创建 MethodChannel,用于与 Flutter 通信
methodChannel = FlutterMethodChannel(
name: "ios_edge_swipe",
binaryMessenger: controller.binaryMessenger
)
methodChannel?.setMethodCallHandler { [weak self] (call, result) in
if call.method == "initSwipeListener" {
self?.setupEdgeSwipeGesture(controller: controller)
result(nil)
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// 添加边缘滑动手势识别器
private func setupEdgeSwipeGesture(controller: UIViewController) {
if edgePanRecognizer != nil {
return // 防止重复添加
}
edgePanRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleEdgeSwipe(_:)))
edgePanRecognizer?.edges = [.left] // 监听左边缘
edgePanRecognizer?.delegate = self
controller.view.addGestureRecognizer(edgePanRecognizer!)
print("✅ 左边缘滑动手势监听已添加")
}
// 触发时回调
@objc private func handleEdgeSwipe(_ gesture: UIScreenEdgePanGestureRecognizer) {
if gesture.state == .recognized {
methodChannel?.invokeMethod("onEdgeSwipe", arguments: nil)
print("👈 左边缘滑动检测到!已通知 Flutter")
}
}
// To be deprecated,please use the new field businessID below.
@objc func offlinePushCertificateID() -> Int32 {
return TencentCloudChatPushFlutterModal.shared.offlinePushCertificateID();
}
// Add this function
@objc func businessID() -> Int32 {
return TencentCloudChatPushFlutterModal.shared.businessID();
}
// Add this function
@objc func applicationGroupID() -> String {
return TencentCloudChatPushFlutterModal.shared.applicationGroupID()
}
// Add this function
@objc func onRemoteNotificationReceived(_ notice: String?) -> Bool {
TencentCloudChatPushPlugin.shared.tryNotifyDartOnNotificationClickEvent(notice)
return true
}
private func getReceiptBase64(result: @escaping FlutterResult) {
guard let receiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: receiptURL.path),
let receiptData = try? Data(contentsOf: receiptURL) else {
result(FlutterError(code: "NO_RECEIPT",
message: "收据不存在,请先刷新",
details: nil))
return
}
result(receiptData.base64EncodedString())
}
private func refreshReceipt(result: @escaping FlutterResult) {
let request = SKReceiptRefreshRequest()
request.delegate = RefreshDelegate(result: result)
request.start()
}
}
// 可选:防止冲突的手势识别设置
extension AppDelegate: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// 如果需要同时识别,比如与 WebView 的滚动手势共存
return true
}
}
class RefreshDelegate: NSObject, SKRequestDelegate {
let result: FlutterResult
init(result: @escaping FlutterResult) { self.result = result }
func requestDidFinish(_ request: SKRequest) {
result(true)
}
func request(_ request: SKRequest, didFailWithError error: Error) {
result(FlutterError(code: "REFRESH_FAILED",
message: error.localizedDescription,
details: nil))
}
}