Skip to content

Commit

Permalink
Fix AccessingSecurityScopedResource
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Dec 18, 2024
1 parent da8e55e commit 5710111
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,19 @@ class BookmarksImportExportUtility {
// While accessing document URL from UIDocumentPickerViewController to access the file
// startAccessingSecurityScopedResource should be called for that URL
// Reference: https://stackoverflow.com/a/73912499/2239348
guard path.startAccessingSecurityScopedResource() else {
return false
let hasSecurityAccess = path.startAccessingSecurityScopedResource()
defer {
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
if hasSecurityAccess {
path.stopAccessingSecurityScopedResource()
}
}

state = .importing
defer { state = .none }

if path.pathExtension.lowercased() == "zip" {
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }

guard let zipFileExtractedURL = try? await ZipImporter.unzip(path: path) else {
return false
}
Expand All @@ -125,9 +126,6 @@ class BookmarksImportExportUtility {
return await doImport(bookmarksFileURL, nativeBookmarksPath)
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }
return await doImport(path, nativePath)
}

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ class HistoryImportExportUtility {
// While accessing document URL from UIDocumentPickerViewController to access the file
// startAccessingSecurityScopedResource should be called for that URL
// Reference: https://stackoverflow.com/a/73912499/2239348
guard path.startAccessingSecurityScopedResource() else {
return false
let hasSecurityAccess = path.startAccessingSecurityScopedResource()
defer {
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
if hasSecurityAccess {
path.stopAccessingSecurityScopedResource()
}
}

state = .importing
defer { state = .none }

if path.pathExtension.lowercased() == "zip" {
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }

guard let zipFileExtractedURL = try? await ZipImporter.unzip(path: path) else {
return false
}
Expand All @@ -88,9 +89,6 @@ class HistoryImportExportUtility {
return await doImport(historyFileURL, nativeHistoryPath)
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }
return await doImport(path, nativePath)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ class PasswordsImportExportUtility {
// While accessing document URL from UIDocumentPickerViewController to access the file
// startAccessingSecurityScopedResource should be called for that URL
// Reference: https://stackoverflow.com/a/73912499/2239348
guard path.startAccessingSecurityScopedResource() else {
return false
let hasSecurityAccess = path.startAccessingSecurityScopedResource()
defer {
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
if hasSecurityAccess {
path.stopAccessingSecurityScopedResource()
}
}

state = .importing
defer { state = .none }

if path.pathExtension.lowercased() == "zip" {
// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }

guard let zipFileExtractedURL = try? await ZipImporter.unzip(path: path) else {
return false
}
Expand All @@ -80,9 +81,6 @@ class PasswordsImportExportUtility {
return await doImport(passwordsFileURL, nativePasswordsPath)
}

// Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource
// (Note: this is not reference counted)
defer { path.stopAccessingSecurityScopedResource() }
return await doImport(path, nativePath)
}

Expand Down

0 comments on commit 5710111

Please sign in to comment.