From 030d86c1b7a2baf28387b5bb6d608d2a847d7683 Mon Sep 17 00:00:00 2001 From: Daniel Jalkut Date: Sat, 26 Oct 2019 14:42:11 -0400 Subject: [PATCH] Use the NSCachesDirectory default location, which for sandboxed builds will end up inside our sandbox container. --- Mac/AppDelegate.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 52eedd75e..e5a678c01 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -163,9 +163,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, } } - let tempDirectory = NSTemporaryDirectory() - let bundleIdentifier = (Bundle.main.infoDictionary!["CFBundleIdentifier"]! as! String) - let cacheFolder = (tempDirectory as NSString).appendingPathComponent(bundleIdentifier) + // Try to establish a cache in the Caches folder, but if it fails for some reason fall back to a temporary dir + let cacheFolder: String + if let userCacheFolder = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path { + cacheFolder = userCacheFolder + } + else { + let bundleIdentifier = (Bundle.main.infoDictionary!["CFBundleIdentifier"]! as! String) + cacheFolder = (NSTemporaryDirectory() as NSString).appendingPathComponent(bundleIdentifier) + } // If the image disk cache hasn't been flushed for 3 days and the network is available, delete it if let flushDate = AppDefaults.lastImageCacheFlushDate, flushDate.addingTimeInterval(3600*24*3) < Date() {