forked from leanflutter/protocol_handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocolHandlerPlugin.swift
More file actions
54 lines (48 loc) · 1.84 KB
/
Copy pathProtocolHandlerPlugin.swift
File metadata and controls
54 lines (48 loc) · 1.84 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
import Cocoa
import FlutterMacOS
public class ProtocolHandlerPlugin: NSObject, FlutterPlugin,FlutterAppLifecycleDelegate {
private static var _instance: ProtocolHandlerPlugin?
private var channel: FlutterMethodChannel!
private var _initialUrl: String?
public static var instance: ProtocolHandlerPlugin {
get {
return _instance!
}
}
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)
registrar.addApplicationDelegate(instance)
}
public func handleWillFinishLaunching(_ notification: Notification) {
NSAppleEventManager.shared().setEventHandler(
self,
andSelector: #selector(handleURLEvent(_:with:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL)
)
}
@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)
}
}
}