-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProtocolHandlerPlugin.swift
More file actions
50 lines (43 loc) · 1.72 KB
/
Copy pathProtocolHandlerPlugin.swift
File metadata and controls
50 lines (43 loc) · 1.72 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
import Cocoa
import FlutterMacOS
public class ProtocolHandlerPlugin: NSObject, FlutterPlugin {
private static var _instance: ProtocolHandlerPlugin?
private var channel: FlutterMethodChannel!
private var _initialUrl: String?
public static var instance: ProtocolHandlerPlugin {
get {
return _instance!
}
}
override init(){
super.init();
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(handleURLEvent(_:with:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "protocol_handler", binaryMessenger: registrar.messenger)
let instance = ProtocolHandlerPlugin()
_instance = instance
instance.channel = channel
registrar.addMethodCallDelegate(instance, channel: channel)
}
@objc
public func handleURLEvent(_ event: NSAppleEventDescriptor, with replyEvent: NSAppleEventDescriptor) {
guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue else { return }
if (_initialUrl == nil) {
_initialUrl = urlString
}
let args: NSDictionary = [
"url": urlString,
]
channel.invokeMethod("onProtocolUrlReceived", arguments: args, result: nil)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getInitialUrl":
result(self._initialUrl ?? "");
break;
default:
result(FlutterMethodNotImplemented)
}
}
}