Fix theme-importing — use startAccessingSecurityScopedResource and stopAccessingSecurityScopedResource, which appear to be required now.

This commit is contained in:
Brent Simmons
2024-12-31 21:15:51 -08:00
parent ed3cf35dbf
commit 8a45b319f7
4 changed files with 56 additions and 36 deletions

View File

@@ -10,9 +10,9 @@ import UIKit
struct ArticleThemeImporter {
static func importTheme(controller: UIViewController, filename: String) throws {
let theme = try ArticleTheme(path: filename, isAppTheme: false)
static func importTheme(controller: UIViewController, url: URL) throws {
let theme = try ArticleTheme(url: url, isAppTheme: false)
let localizedTitleText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text")
let title = NSString.localizedStringWithFormat(localizedTitleText as NSString, theme.name, theme.creatorName) as String
@@ -24,18 +24,24 @@ struct ArticleThemeImporter {
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel")
alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel))
if let url = URL(string: theme.creatorHomePage) {
if let websiteURL = URL(string: theme.creatorHomePage) {
let visitSiteTitle = NSLocalizedString("Show Website", comment: "Show Website")
let visitSiteAction = UIAlertAction(title: visitSiteTitle, style: .default) { action in
UIApplication.shared.open(url)
try? Self.importTheme(controller: controller, filename: filename)
UIApplication.shared.open(websiteURL)
try? Self.importTheme(controller: controller, url: url)
}
alertController.addAction(visitSiteAction)
}
func importTheme() {
_ = url.startAccessingSecurityScopedResource()
defer {
url.stopAccessingSecurityScopedResource()
}
do {
try ArticleThemesManager.shared.importTheme(filename: filename)
try ArticleThemesManager.shared.importTheme(filename: url.path)
confirmImportSuccess(controller: controller, themeName: theme.name)
} catch {
controller.presentError(error)
@@ -45,7 +51,7 @@ struct ArticleThemeImporter {
let installThemeTitle = NSLocalizedString("Install Theme", comment: "Install Theme")
let installThemeAction = UIAlertAction(title: installThemeTitle, style: .default) { action in
if ArticleThemesManager.shared.themeExists(filename: filename) {
if ArticleThemesManager.shared.themeExists(filename: url.path) {
let title = NSLocalizedString("Duplicate Theme", comment: "Duplicate Theme")
let localizedMessageText = NSLocalizedString("The theme “%@” already exists. Overwrite it?", comment: "Overwrite theme")
let message = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name) as String

View File

@@ -117,10 +117,18 @@ extension ArticleThemesTableViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
do {
try ArticleThemeImporter.importTheme(controller: self, filename: url.standardizedFileURL.path)
} catch {
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
if url.startAccessingSecurityScopedResource() {
defer {
url.stopAccessingSecurityScopedResource()
}
do {
try ArticleThemeImporter.importTheme(controller: self, url: url)
} catch {
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error])
}
}
}
}