Skip to content

Instantly share code, notes, and snippets.

@gee1k
Created January 19, 2021 06:58
Show Gist options
  • Save gee1k/9e7e547c0f7d3dc3306fd5c0d4765d6f to your computer and use it in GitHub Desktop.
Save gee1k/9e7e547c0f7d3dc3306fd5c0d4765d6f to your computer and use it in GitHub Desktop.
//
// DiskPermissionManager.swift
// uPic
//
// Created by Svend Jin on 2021/01/19.
// Copyright © 2021 Svend Jin. All rights reserved.
//
// entitlements 文件加入
// <key>com.apple.security.files.user-selected.read-only</key>
// <true/>
// <key>com.apple.security.files.bookmarks.app-scope</key>
// <true/>
import Foundation
import Cocoa
public class DiskPermissionManager {
// static
public static var shared = DiskPermissionManager()
private func promptForWorkingDirectoryPermission() -> URL? {
let openPanel = NSOpenPanel()
openPanel.message = "Authorize".localized
openPanel.prompt = "Authorize".localized
openPanel.allowedFileTypes = ["none"]
openPanel.allowsOtherFileTypes = false
openPanel.canChooseFiles = false
openPanel.canCreateDirectories = false
openPanel.canChooseDirectories = true
openPanel.directoryURL = URL(fileURLWithPath: "/", isDirectory: true)
openPanel.runModal()
return openPanel.urls.first
}
private func saveBookmarkData(for workDir: URL) {
do {
let bookmarkData = try workDir.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
// save in UserDefaults
Defaults[.workingDirectoryBookmark] = bookmarkData
} catch {
print("Failed to save bookmark data for \(workDir)", error)
}
}
private func restoreFileAccess(with bookmarkData: Data) -> URL? {
do {
var isStale = false
let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
if isStale {
// bookmarks could become stale as the OS changes
print("Bookmark is stale, need to save a new one... ")
saveBookmarkData(for: url)
}
return url
} catch {
print("Error resolving bookmark:", error)
return nil
}
}
}
// Utils
extension DiskPermissionManager {
func initializeRequestDiskPermissions() {
if !Defaults[.requestedAuthorization] {
Defaults[.requestedAuthorization] = true
alertInfo(withText: "Full Disk Access".localized, withMessage: "Full Disk Access Message".localized, oKButtonTitle: "Authorize".localized, cancelButtonTitle: "Later".localized){ [self] in
requestFullDiskPermissions()
}
} else {
guard let data = Defaults[.workingDirectoryBookmark], let url = restoreFileAccess(with: data) else {
return
}
_ = url.startAccessingSecurityScopedResource()
}
}
func checkFullDiskAuthorizationStatus() -> Bool {
guard let data = Defaults[.workingDirectoryBookmark] else {
return false
}
do {
var isStale = true
let url = try URL(resolvingBookmarkData: data, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
if isStale {
// bookmarks could become stale as the OS changes
print("Bookmark is stale, need to save a new one... ")
return false
}
return url.path == "/"
} catch {
print("Error resolving bookmark:", error)
return false
}
}
func requestFullDiskPermissions() {
guard let url = self.promptForWorkingDirectoryPermission() else {
return
}
self.saveBookmarkData(for: url)
_ = url.startAccessingSecurityScopedResource()
}
func stopFullDiskAccessing() {
guard let data = Defaults[.workingDirectoryBookmark], let url = restoreFileAccess(with: data) else {
return
}
url.stopAccessingSecurityScopedResource()
}
}
@Sloaix
Copy link

Sloaix commented Jun 30, 2021

macOS应用开发的必备路径:

  • 面向Google编程
  • 面向StackOverFlow编程
  • 面相Github Gist编程
  • 最后面向Apple开发者文档编程

:) 幸运的是,我在第三步,找到了自己想要的答案,原来只要启动应用的时候默认获取根目录访问权限,然后用书签保存下来即可!

谢谢这个有用的分享!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment