-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathPosthogFlutterPlugin.swift
More file actions
771 lines (686 loc) · 26.4 KB
/
PosthogFlutterPlugin.swift
File metadata and controls
771 lines (686 loc) · 26.4 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import PostHog
#if os(iOS)
import Flutter
import UIKit
#elseif os(macOS)
import AppKit
import FlutterMacOS
#endif
public class PosthogFlutterPlugin: NSObject, FlutterPlugin {
private static var instance: PosthogFlutterPlugin?
private var channel: FlutterMethodChannel?
public static func getInstance() -> PosthogFlutterPlugin? {
instance
}
override init() {
super.init()
NotificationCenter.default.addObserver(
self,
selector: #selector(featureFlagsDidUpdate),
name: PostHogSDK.didReceiveFeatureFlags,
object: nil
)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
public static func register(with registrar: FlutterPluginRegistrar) {
let methodChannel: FlutterMethodChannel
#if os(iOS)
methodChannel = FlutterMethodChannel(name: "posthog_flutter", binaryMessenger: registrar.messenger())
#elseif os(macOS)
methodChannel = FlutterMethodChannel(name: "posthog_flutter", binaryMessenger: registrar.messenger)
#endif
let instance = PosthogFlutterPlugin()
instance.channel = methodChannel
PosthogFlutterPlugin.instance = instance
initPlugin()
registrar.addMethodCallDelegate(instance, channel: methodChannel)
}
@objc func featureFlagsDidUpdate() {
invokeFlutterMethod("onFeatureFlagsCallback", arguments: [String: Any]())
}
private let dispatchQueue = DispatchQueue(label: "com.posthog.PosthogFlutterPlugin",
target: .global(qos: .utility))
public static func initPlugin() {
let autoInit = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.AUTO_INIT") as? Bool ?? true
if !autoInit {
print("[PostHog] com.posthog.posthog.AUTO_INIT is disabled!")
return
}
let apiKey = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.API_KEY") as? String ?? ""
let host = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.POSTHOG_HOST") as? String ?? PostHogConfig.defaultHost
let captureApplicationLifecycleEvents = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.CAPTURE_APPLICATION_LIFECYCLE_EVENTS") as? Bool ?? false
let debug = Bundle.main.object(forInfoDictionaryKey: "com.posthog.posthog.DEBUG") as? Bool ?? false
setupPostHog([
"apiKey": apiKey,
"host": host,
"captureApplicationLifecycleEvents": captureApplicationLifecycleEvents,
"debug": debug,
])
}
private static func setupPostHog(_ posthogConfig: [String: Any]) {
guard let instance = PosthogFlutterPlugin.instance else {
print("[PostHog] Plugin instance not found!")
return
}
let apiKey = posthogConfig["apiKey"] as? String ?? ""
if apiKey.isEmpty {
print("[PostHog] apiKey is missing!")
return
}
let host = posthogConfig["host"] as? String ?? PostHogConfig.defaultHost
let config = PostHogConfig(
apiKey: apiKey,
host: host
)
config.captureScreenViews = false
if let captureApplicationLifecycleEvents = posthogConfig["captureApplicationLifecycleEvents"] as? Bool {
config.captureApplicationLifecycleEvents = captureApplicationLifecycleEvents
}
if let debug = posthogConfig["debug"] as? Bool {
config.debug = debug
}
if let flushAt = posthogConfig["flushAt"] as? Int {
config.flushAt = flushAt
}
if let maxQueueSize = posthogConfig["maxQueueSize"] as? Int {
config.maxQueueSize = maxQueueSize
}
if let maxBatchSize = posthogConfig["maxBatchSize"] as? Int {
config.maxBatchSize = maxBatchSize
}
if let flushInterval = posthogConfig["flushInterval"] as? Int {
config.flushIntervalSeconds = Double(flushInterval)
}
if let sendFeatureFlagEvents = posthogConfig["sendFeatureFlagEvents"] as? Bool {
config.sendFeatureFlagEvent = sendFeatureFlagEvents
}
if let preloadFeatureFlags = posthogConfig["preloadFeatureFlags"] as? Bool {
config.preloadFeatureFlags = preloadFeatureFlags
}
if let optOut = posthogConfig["optOut"] as? Bool {
config.optOut = optOut
}
if let personProfiles = posthogConfig["personProfiles"] as? String {
switch personProfiles {
case "never":
config.personProfiles = .never
case "always":
config.personProfiles = .always
case "identifiedOnly":
config.personProfiles = .identifiedOnly
default:
break
}
}
if let dataMode = posthogConfig["dataMode"] as? String {
switch dataMode {
case "wifi":
config.dataMode = .wifi
case "cellular":
config.dataMode = .cellular
case "any":
config.dataMode = .any
default:
break
}
}
#if os(iOS)
// configure session replay
if let sessionReplay = posthogConfig["sessionReplay"] as? Bool {
config.sessionReplay = sessionReplay
}
// disabled since Dart has native libs such as http/dio and dont use the ios URLSession
config.sessionReplayConfig.captureNetworkTelemetry = false
// configure surveys
if #available(iOS 15.0, *) {
let surveys: Bool = posthogConfig["surveys"] as? Bool ?? false
config.surveys = surveys
if surveys {
// if surveys are enabled, assign this instance as the survey delegate (we'll take over rendering)
config.surveysConfig.surveysDelegate = instance
}
}
#endif
// Update SDK name and version
postHogSdkName = "posthog-flutter"
postHogVersion = postHogFlutterVersion
PostHogSDK.shared.setup(config)
}
private var currentSurvey: PostHogDisplaySurvey?
private var onSurveyShownCallback: OnPostHogSurveyShown?
private var onSurveyResponseCallback: OnPostHogSurveyResponse?
private var onSurveyClosedCallback: OnPostHogSurveyClosed?
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "setup":
setup(call, result: result)
case "getFeatureFlag":
getFeatureFlag(call, result: result)
case "isFeatureEnabled":
isFeatureEnabled(call, result: result)
case "getFeatureFlagPayload":
getFeatureFlagPayload(call, result: result)
case "getFeatureFlagResult":
getFeatureFlagResult(call, result: result)
case "identify":
identify(call, result: result)
case "capture":
capture(call, result: result)
case "screen":
screen(call, result: result)
case "alias":
alias(call, result: result)
case "distinctId":
distinctId(result)
case "reset":
reset(result)
case "enable":
enable(result)
case "disable":
disable(result)
case "isOptOut":
isOptOut(result)
case "debug":
debug(call, result: result)
case "reloadFeatureFlags":
reloadFeatureFlags(result)
case "group":
group(call, result: result)
case "register":
register(call, result: result)
case "unregister":
unregister(call, result: result)
case "flush":
flush(result)
case "captureException":
captureException(call, result: result)
case "close":
close(result)
case "sendMetaEvent":
sendMetaEvent(call, result: result)
case "sendFullSnapshot":
sendFullSnapshot(call, result: result)
case "isSessionReplayActive":
isSessionReplayActive(result: result)
case "getSessionId":
getSessionId(result: result)
case "openUrl":
openUrl(call, result: result)
case "surveyAction":
#if os(iOS)
handleSurveyAction(call, result: result)
#else
// surveys only supported on iOS
result(nil)
#endif
default:
result(FlutterMethodNotImplemented)
}
}
}
#if os(iOS)
// MARK: - PostHogSurveysDelegate
extension PosthogFlutterPlugin: PostHogSurveysDelegate {
public func renderSurvey(
_ survey: PostHogDisplaySurvey,
onSurveyShown: @escaping OnPostHogSurveyShown,
onSurveyResponse: @escaping OnPostHogSurveyResponse,
onSurveyClosed: @escaping OnPostHogSurveyClosed
) {
// Store the callbacks and survey for later use
currentSurvey = survey
onSurveyShownCallback = onSurveyShown
onSurveyResponseCallback = onSurveyResponse
onSurveyClosedCallback = onSurveyClosed
// We don't need to handle the result here
// All responses will come through the surveyResponse method
invokeFlutterMethod("showSurvey", arguments: survey.toDict())
}
public func cleanupSurveys() {
// Reset all survey-related state when the survey feature is stopped
currentSurvey = nil
onSurveyShownCallback = nil
onSurveyResponseCallback = nil
onSurveyClosedCallback = nil
// Notify Flutter side that surveys have been cleaned up
invokeFlutterMethod("hideSurveys", arguments: nil)
}
private func handleSurveyAction(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let survey = currentSurvey,
let args = call.arguments as? [String: Any],
let type = args["type"] as? String
else {
result(FlutterError(code: "InvalidArguments", message: "Invalid survey action arguments", details: nil))
return
}
switch type {
case "shown":
onSurveyShownCallback?(survey)
case "response":
if let index = args["index"] as? Int,
index < survey.questions.count
{
let question = survey.questions[index]
let responsePayload = args["response"]
// Create PostHogSurveyResponse based on question type
var surveyResponse: PostHogSurveyResponse
switch question {
case is PostHogDisplayLinkQuestion:
// For link questions
let boolValue = responsePayload as? Bool ?? false
surveyResponse = .link(boolValue)
case is PostHogDisplayRatingQuestion:
// For rating questions
let ratingValue = responsePayload as? Int
surveyResponse = .rating(ratingValue)
case let choiceQuestion as PostHogDisplayChoiceQuestion:
// For single/multiple choice questions
var selectedOptions: [String]? = nil
if choiceQuestion.isMultipleChoice {
// Multiple choice: accept array directly from Flutter
selectedOptions = responsePayload as? [String]
surveyResponse = .multipleChoice(selectedOptions)
} else {
// Single choice: Flutter sends as a list with one element
selectedOptions = responsePayload as? [String]
surveyResponse = .singleChoice(selectedOptions?.first)
}
default:
// Default to open text question
let textValue = responsePayload as? String
surveyResponse = .openEnded(textValue)
}
// Call the callback with the constructed response
if let nextQuestion = onSurveyResponseCallback?(survey, index, surveyResponse) {
result(["nextIndex": nextQuestion.questionIndex,
"isSurveyCompleted": nextQuestion.isSurveyCompleted])
return
}
}
case "closed":
onSurveyClosedCallback?(survey)
// Clear the callbacks after survey is closed
currentSurvey = nil
onSurveyShownCallback = nil
onSurveyResponseCallback = nil
onSurveyClosedCallback = nil
default:
break
}
result(nil)
}
}
#endif
extension PosthogFlutterPlugin {
private func sendMetaEvent(_ call: FlutterMethodCall,
result: @escaping FlutterResult)
{
#if os(iOS)
let date = Date()
let timestamp = dateToMillis(date)
if let args = call.arguments as? [String: Any] {
let width = args["width"] as? Int ?? 0
let height = args["height"] as? Int ?? 0
let screen = args["screen"] as? String ?? ""
if width == 0 || height == 0 {
_badArgumentError(result)
return
}
dispatchQueue.async {
var snapshotsData: [Any] = []
let data: [String: Any] = ["width": width, "height": height, "href": screen]
let snapshotData: [String: Any] = ["type": 4, "data": data, "timestamp": timestamp]
snapshotsData.append(snapshotData)
PostHogSDK.shared.capture("$snapshot", properties: ["$snapshot_source": "mobile", "$snapshot_data": snapshotsData], timestamp: date)
}
result(nil)
} else {
_badArgumentError(result)
}
#else
result(nil)
#endif
}
private func sendFullSnapshot(_ call: FlutterMethodCall,
result: @escaping FlutterResult)
{
#if os(iOS)
let date = Date()
let timestamp = dateToMillis(date)
if let args = call.arguments as? [String: Any] {
let id = args["id"] as? Int ?? 1
let x = args["x"] as? Int ?? 0
let y = args["y"] as? Int ?? 0
guard let imageBytes = args["imageBytes"] as? FlutterStandardTypedData else {
_badArgumentError(result)
return
}
dispatchQueue.async {
guard let image = UIImage(data: imageBytes.data) else {
// bad data but we cannot do this in the calling thread
// otherwise we are doing slow operatios in the main thread
return
}
guard let base64 = imageToBase64(image) else {
// bad data but we cannot do this in the calling thread
// otherwise we are doing slow operatios in the main thread
return
}
var snapshotsData: [Any] = []
var wireframes: [Any] = []
let wireframe: [String: Any] = [
"id": id,
"x": x,
"y": y,
"width": Int(image.size.width),
"height": Int(image.size.height),
"type": "screenshot",
"base64": base64,
"style": [:],
]
wireframes.append(wireframe)
let initialOffset = ["top": 0, "left": 0]
let data: [String: Any] = ["initialOffset": initialOffset, "wireframes": wireframes]
let snapshotData: [String: Any] = ["type": 2, "data": data, "timestamp": timestamp]
snapshotsData.append(snapshotData)
PostHogSDK.shared.capture("$snapshot", properties: ["$snapshot_source": "mobile", "$snapshot_data": snapshotsData], timestamp: date)
}
result(nil)
} else {
_badArgumentError(result)
}
#else
result(nil)
#endif
}
private func isSessionReplayActive(result: @escaping FlutterResult) {
#if os(iOS)
result(PostHogSDK.shared.isSessionReplayActive())
#else
result(false)
#endif
}
private func openUrl(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let url = call.arguments as? String,
let urlObject = URL(string: url)
{
#if os(iOS)
if UIApplication.shared.canOpenURL(urlObject) {
UIApplication.shared.open(urlObject)
}
#else
NSWorkspace.shared.open(urlObject)
#endif
result(nil)
} else {
result(FlutterError(code: "InvalidArguments",
message: "Invalid URL",
details: "The URL provided is invalid"))
}
}
private func setup(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any] {
PosthogFlutterPlugin.setupPostHog(args)
result(nil)
} else {
_badArgumentError(result)
}
}
private func getFeatureFlag(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let featureFlagKey = args["key"] as? String
{
let value = PostHogSDK.shared.getFeatureFlag(featureFlagKey)
result(value)
} else {
_badArgumentError(result)
}
}
private func isFeatureEnabled(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let featureFlagKey = args["key"] as? String
{
let value = PostHogSDK.shared.isFeatureEnabled(featureFlagKey)
result(value)
} else {
_badArgumentError(result)
}
}
private func getFeatureFlagPayload(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let featureFlagKey = args["key"] as? String
{
let value = PostHogSDK.shared.getFeatureFlagPayload(featureFlagKey)
result(value)
} else {
_badArgumentError(result)
}
}
private func getFeatureFlagResult(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let featureFlagKey = args["key"] as? String
{
let sendEvent = args["sendEvent"] as? Bool ?? true
let flagResult = PostHogSDK.shared.getFeatureFlagResult(featureFlagKey, sendFeatureFlagEvent: sendEvent)
if let flagResult {
result([
"key": flagResult.key,
"enabled": flagResult.enabled,
"variant": flagResult.variant as Any,
"payload": flagResult.payload as Any
])
} else {
result(nil)
}
} else {
_badArgumentError(result)
}
}
private func identify(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let userId = args["userId"] as? String
{
let userProperties = args["userProperties"] as? [String: Any]
let userPropertiesSetOnce = args["userPropertiesSetOnce"] as? [String: Any]
PostHogSDK.shared.identify(
userId,
userProperties: userProperties,
userPropertiesSetOnce: userPropertiesSetOnce
)
result(nil)
} else {
_badArgumentError(result)
}
}
private func capture(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let eventName = args["eventName"] as? String
{
let properties = args["properties"] as? [String: Any]
let userProperties = args["userProperties"] as? [String: Any]
let userPropertiesSetOnce = args["userPropertiesSetOnce"] as? [String: Any]
PostHogSDK.shared.capture(
eventName,
properties: properties,
userProperties: userProperties,
userPropertiesSetOnce: userPropertiesSetOnce
)
result(nil)
} else {
_badArgumentError(result)
}
}
private func screen(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let screenName = args["screenName"] as? String
{
let properties = args["properties"] as? [String: Any]
PostHogSDK.shared.screen(
screenName,
properties: properties
)
result(nil)
} else {
_badArgumentError(result)
}
}
private func alias(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let alias = args["alias"] as? String
{
PostHogSDK.shared.alias(alias)
result(nil)
} else {
_badArgumentError(result)
}
}
private func distinctId(_ result: @escaping FlutterResult) {
let val = PostHogSDK.shared.getDistinctId()
result(val)
}
private func reset(_ result: @escaping FlutterResult) {
PostHogSDK.shared.reset()
result(nil)
}
private func enable(_ result: @escaping FlutterResult) {
PostHogSDK.shared.optIn()
result(nil)
}
private func disable(_ result: @escaping FlutterResult) {
PostHogSDK.shared.optOut()
result(nil)
}
private func isOptOut(_ result: @escaping FlutterResult) {
let isOptedOut = PostHogSDK.shared.isOptOut()
result(isOptedOut)
}
private func debug(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let debug = args["debug"] as? Bool
{
PostHogSDK.shared.debug(debug)
result(nil)
} else {
_badArgumentError(result)
}
}
private func reloadFeatureFlags(_ result: @escaping FlutterResult
) {
PostHogSDK.shared.reloadFeatureFlags()
result(nil)
}
private func group(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let groupType = args["groupType"] as? String,
let groupKey = args["groupKey"] as? String
{
let groupProperties = args["groupProperties"] as? [String: Any]
PostHogSDK.shared.group(type: groupType, key: groupKey, groupProperties: groupProperties)
result(nil)
} else {
_badArgumentError(result)
}
}
private func register(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let key = args["key"] as? String,
let value = args["value"]
{
PostHogSDK.shared.register([key: value])
result(nil)
} else {
_badArgumentError(result)
}
}
private func unregister(
_ call: FlutterMethodCall,
result: @escaping FlutterResult
) {
if let args = call.arguments as? [String: Any],
let key = args["key"] as? String
{
PostHogSDK.shared.unregister(key)
result(nil)
} else {
_badArgumentError(result)
}
}
private func flush(_ result: @escaping FlutterResult) {
PostHogSDK.shared.flush()
result(nil)
}
private func captureException(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let arguments = call.arguments as? [String: Any] else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "Invalid arguments for captureException", details: nil))
return
}
let properties = arguments["properties"] as? [String: Any]
// Extract timestamp from Flutter and convert to Date
var timestamp: Date? = nil
if let timestampMs = arguments["timestamp"] as? Int64 {
timestamp = Date(timeIntervalSince1970: TimeInterval(timestampMs) / 1000.0)
}
// Use capture method with timestamp to ensure Flutter timestamp is used
PostHogSDK.shared.capture("$exception", properties: properties, timestamp: timestamp)
result(nil)
}
private func close(_ result: @escaping FlutterResult) {
PostHogSDK.shared.close()
result(nil)
}
private func getSessionId(result: @escaping FlutterResult) {
result(PostHogSDK.shared.getSessionId())
}
// Return bad Arguments error
private func _badArgumentError(_ result: @escaping FlutterResult) {
result(FlutterError(
code: "PosthogFlutterException", message: "Missing arguments!", details: nil
))
}
private func invokeFlutterMethod(_ method: String, arguments: Any? = nil) {
DispatchQueue.main.async { [weak self] in
self?.channel?.invokeMethod(method, arguments: arguments)
}
}
}