Skip to content

Commit 45063b4

Browse files
committed
[macos] Recovery isAccessAllowed and requestAccess methods
1 parent dcd2310 commit 45063b4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/src/screen_text_extractor.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22
import 'dart:io';
33

4+
import 'package:flutter/foundation.dart';
45
import 'package:flutter/services.dart';
56

67
import 'extract_mode.dart';
@@ -17,6 +18,24 @@ class ScreenTextExtractor {
1718

1819
ClipboardOnceWatcher _clipboardOnceWatcher = ClipboardOnceWatcher();
1920

21+
Future<bool> isAccessAllowed() async {
22+
if (!kIsWeb && Platform.isMacOS) {
23+
return await _channel.invokeMethod('isAccessAllowed');
24+
}
25+
return true;
26+
}
27+
28+
Future<void> requestAccess({
29+
bool onlyOpenPrefPane = false,
30+
}) async {
31+
if (!kIsWeb && Platform.isMacOS) {
32+
final Map<String, dynamic> arguments = {
33+
'onlyOpenPrefPane': onlyOpenPrefPane,
34+
};
35+
await _channel.invokeMethod('requestAccess', arguments);
36+
}
37+
}
38+
2039
Future<ExtractedData?> extract({
2140
ExtractMode mode = ExtractMode.clipboard,
2241
}) async {
@@ -68,3 +87,5 @@ class ScreenTextExtractor {
6887
}
6988
}
7089
}
90+
91+
final screenTextExtractor = ScreenTextExtractor.instance;

macos/Classes/ScreenTextExtractorPlugin.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,37 @@ public class ScreenTextExtractorPlugin: NSObject, FlutterPlugin {
1111

1212
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
1313
switch call.method {
14+
case "isAccessAllowed":
15+
isAccessAllowed(call, result: result)
16+
break
17+
case "requestAccess":
18+
requestAccess(call, result: result)
19+
break
1420
case "simulateCtrlCKeyPress":
1521
simulateCtrlCKeyPress(call, result: result)
1622
break
1723
default:
1824
result(FlutterMethodNotImplemented)
1925
}
2026
}
27+
28+
public func isAccessAllowed(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
29+
result(AXIsProcessTrusted())
30+
}
31+
32+
public func requestAccess(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
33+
let args:[String: Any] = call.arguments as! [String: Any]
34+
let onlyOpenPrefPane: Bool = args["onlyOpenPrefPane"] as! Bool
35+
36+
if (!onlyOpenPrefPane) {
37+
let options = [kAXTrustedCheckOptionPrompt.takeRetainedValue(): true] as CFDictionary
38+
AXIsProcessTrustedWithOptions(options)
39+
} else {
40+
let prefpaneUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!
41+
NSWorkspace.shared.open(prefpaneUrl)
42+
}
43+
result(true)
44+
}
2145

2246
public func simulateCtrlCKeyPress(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
2347
let eventKeyDown = CGEvent(keyboardEventSource: nil, virtualKey: CGKeyCode(UInt32(kVK_ANSI_C)), keyDown: true);

0 commit comments

Comments
 (0)