-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
macapp: prevent screen capture nags after permission granted
- Loading branch information
Showing
9 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
macapp/App/Sources/App/Dependencies/DeviceClient/DeviceClient+ScreenCaptureNag.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Foundation | ||
|
||
@Sendable func _preventScreenCaptureNag() async -> Result<Void, AppError> { | ||
let macosVersion = macOSVersion().semver | ||
guard macosVersion.major >= 15 else { | ||
return .success(()) | ||
} | ||
|
||
let path = approvalFilepath() | ||
switch loadPlist(at: path) { | ||
case .failure(let error): | ||
return .failure(error) | ||
case .success(var plist): | ||
// NB: fileformat changed between 15.0 and 15.1, see: | ||
// https://github.com/gertrude-app/project/issues/334#issuecomment-2568295348 | ||
if macosVersion < .init("15.1.0")! { | ||
plist["/Applications/Gertrude.app/Contents/MacOS/Gertrude"] = Date() + .days(90) | ||
} else { | ||
let value: [String: Any] = [ | ||
"kScreenCaptureAlertableUsageCount": Int(1), | ||
"kScreenCaptureApprovalLastAlerted": Date() + .days(90), | ||
"kScreenCaptureApprovalLastUsed": Date() + .days(90), | ||
"kScreenCapturePrivacyHintDate": Date() + .days(90), | ||
] | ||
plist["com.netrivet.gertrude.app"] = value | ||
} | ||
return write(plist, to: path) | ||
} | ||
} | ||
|
||
private func write(_ plist: [String: Any], to url: URL) -> Result<Void, AppError> { | ||
do { | ||
let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) | ||
try data.write(to: url) | ||
return .success(()) | ||
} catch { | ||
return .failure(.init( | ||
oslogging: "error writing plist to file: \(error)", | ||
context: "DeviceClient.preventScreenCaptureNag" | ||
)) | ||
} | ||
} | ||
|
||
private func loadPlist(at url: URL) -> Result<[String: Any], AppError> { | ||
do { | ||
let data = try Data(contentsOf: url) | ||
do { | ||
guard let plist = try PropertyListSerialization | ||
.propertyList(from: data, options: [], format: nil) as? [String: Any] else { | ||
return .failure(.init( | ||
oslogging: "got nil casting Data to [String: Any]", | ||
context: "DeviceClient.preventScreenCaptureNag" | ||
)) | ||
} | ||
return .success(plist) | ||
} catch { | ||
return .failure(.init( | ||
oslogging: "error casting Data to [String: Any]: \(error)", | ||
context: "DeviceClient.preventScreenCaptureNag" | ||
)) | ||
} | ||
} catch { | ||
return .failure(.init( | ||
oslogging: "error reading Data from plist file: \(error)", | ||
context: "DeviceClient.preventScreenCaptureNag" | ||
)) | ||
} | ||
} | ||
|
||
private func approvalFilepath() -> URL { | ||
let home = FileManager.default.homeDirectoryForCurrentUser.path | ||
let path = "\(home)/Library/Group Cont" + | ||
"ainers/gro" + "up.com.apple.repl" + | ||
"ayd/Screen" + "CaptureApp" + "rovals.plist" | ||
return URL(fileURLWithPath: path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters