Skip to content

Commit ed7916a

Browse files
committed
[macos] Implement getInitialUrl metnod
1 parent 0486388 commit ed7916a

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

example/lib/pages/home.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ class HomePage extends StatefulWidget {
1414
class _HomePageState extends State<HomePage> with ProtocolListener {
1515
bool _isAlwaysOnTop = false;
1616

17+
String? _initialUrl = '';
1718
final List<String> _receivedUrlList = [];
1819

1920
@override
2021
void initState() {
2122
protocolHandler.addListener(this);
2223
super.initState();
24+
25+
_init();
2326
}
2427

2528
@override
@@ -28,6 +31,11 @@ class _HomePageState extends State<HomePage> with ProtocolListener {
2831
super.dispose();
2932
}
3033

34+
void _init() async {
35+
_initialUrl = await protocolHandler.getInitialUrl();
36+
setState(() {});
37+
}
38+
3139
Widget _buildBody(BuildContext context) {
3240
return PreferenceList(
3341
children: <Widget>[
@@ -44,6 +52,20 @@ class _HomePageState extends State<HomePage> with ProtocolListener {
4452
),
4553
],
4654
),
55+
PreferenceListSection(
56+
title: const Text('initialUrl'),
57+
children: [
58+
PreferenceListItem(
59+
padding: const EdgeInsets.all(12),
60+
title: Text('${_initialUrl}'),
61+
accessoryView: Container(),
62+
onTap: () async {
63+
_initialUrl = await protocolHandler.getInitialUrl();
64+
setState(() {});
65+
},
66+
),
67+
],
68+
),
4769
PreferenceListSection(
4870
title: const Text('Received urls'),
4971
children: [

example/macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import FlutterMacOS
66
import Foundation
77

88
import protocol_handler
9+
import window_manager
910

1011
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
1112
ProtocolHandlerPlugin.register(with: registry.registrar(forPlugin: "ProtocolHandlerPlugin"))
13+
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
1214
}

example/macos/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ PODS:
22
- FlutterMacOS (1.0.0)
33
- protocol_handler (0.0.1):
44
- FlutterMacOS
5+
- window_manager (0.2.0):
6+
- FlutterMacOS
57

68
DEPENDENCIES:
79
- FlutterMacOS (from `Flutter/ephemeral`)
810
- protocol_handler (from `Flutter/ephemeral/.symlinks/plugins/protocol_handler/macos`)
11+
- window_manager (from `Flutter/ephemeral/.symlinks/plugins/window_manager/macos`)
912

1013
EXTERNAL SOURCES:
1114
FlutterMacOS:
1215
:path: Flutter/ephemeral
1316
protocol_handler:
1417
:path: Flutter/ephemeral/.symlinks/plugins/protocol_handler/macos
18+
window_manager:
19+
:path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos
1520

1621
SPEC CHECKSUMS:
1722
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
1823
protocol_handler: 587e1caf6c0b92ce351ab14081968dae49cb8cc6
24+
window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8
1925

2026
PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c
2127

lib/src/protocol_handler.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,16 @@ class ProtocolHandler {
5151
_listeners.remove(listener);
5252
}
5353

54+
/// Register a custom protocol
5455
Future<void> register(String scheme) {
5556
return protocolRegistrar.register(scheme);
5657
}
58+
59+
/// If the app launch was triggered by an protocol, it will give the link url, otherwise it will give null.
60+
Future<String?> getInitialUrl() async {
61+
String initialUrl = await _channel.invokeMethod('getInitialUrl');
62+
return initialUrl.isEmpty ? null : initialUrl;
63+
}
5764
}
5865

5966
final protocolHandler = ProtocolHandler.instance;

macos/Classes/ProtocolHandlerPlugin.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import FlutterMacOS
44
public class ProtocolHandlerPlugin: NSObject, FlutterPlugin {
55
private var channel: FlutterMethodChannel!
66

7+
private var _initialUrl: String?
8+
79
override init(){
810
super.init();
911
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(handleURLEvent(_:with:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
@@ -19,22 +21,21 @@ public class ProtocolHandlerPlugin: NSObject, FlutterPlugin {
1921
@objc
2022
private func handleURLEvent(_ event: NSAppleEventDescriptor, with replyEvent: NSAppleEventDescriptor) {
2123
guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue else { return }
24+
if (_initialUrl == nil) {
25+
_initialUrl = urlString
26+
}
2227

2328
let args: NSDictionary = [
2429
"url": urlString,
2530
]
2631
channel.invokeMethod("onProtocolUrlReceived", arguments: args, result: nil)
2732
}
2833

29-
public func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool {
30-
if (userActivity.activityType == NSUserActivityTypeBrowsingWeb) {
31-
// TODO
32-
}
33-
return false
34-
}
35-
3634
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
3735
switch call.method {
36+
case "getInitialUrl":
37+
result(self._initialUrl ?? "");
38+
break;
3839
default:
3940
result(FlutterMethodNotImplemented)
4041
}

0 commit comments

Comments
 (0)