-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathXcodeAccessView.swift
42 lines (37 loc) · 1.71 KB
/
XcodeAccessView.swift
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
import AppKit
class XcodeAccessView: NSView {
@IBOutlet var messageField: NSTextField!
@IBOutlet var fixButton: NSButton!
static var xcodeAccess: XcodeAccess = XcodeAccessImpl()
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
NotificationCenter.default.addObserver(self, selector: #selector(updateView), name: NSNotification.Name(rawValue: "NSApplicationDidBecomeActiveNotification"), object: nil)
updateView()
}
@objc func updateView() {
handleStatus(XcodeAccessView.xcodeAccess.request())
}
private func handleStatus(_ status: XcodeAccessStatus) {
let message: String
var hideFixButton = true
switch status {
case .granted:
message = "✔︎ Mock Generator has access to Xcode"
case .denied:
hideFixButton = false
message = "⚠️ Mock Generator does not have permission to access Xcode.\nGo to System Preferences -> Security & Privacy -> Privacy -> Automation and make sure this app is allowed to control Xcode.\nThen restart the app."
case .notRunning:
message = "Open Xcode to check if Mock Generator has access to Xcode"
case .unknown(let code):
message = "⚠️ Mock Generator does not have permission to access Xcode for an unknown reason (\(code))"
case .requiresConsent:
message = "Requesting access to Xcode..."
}
messageField.stringValue = message
fixButton.isHidden = hideFixButton
}
@IBAction func didTapFixButton(_ sender: Any) {
let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Automation")!
NSWorkspace.shared.open(url)
}
}