When I upgraded from Xcode 8 to 9, existing code that creates a directory started to fail. This code fails on Xcode 9 on a tvOS device. It runs fine in the simulator, and in Xcode 8 it runs fine in the simulator and on a device.
It's easy to repro. Create a new tvOS project with Xcode 9 with the following app delegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
var url = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
url = url.appendingPathComponent("Testing123", isDirectory: true)
print("URL = \(url)")
print("Exists = \(fileExists(url: url))")
do {
try FileManager.default.createDirectory(at: url,
withIntermediateDirectories: true, attributes: nil)
}
catch {
assertionFailure("Could not create \(url): \(error)")
}
return true
}
private func fileExists(url: URL) -> Bool {
return (try? url.checkResourceIsReachable()) ?? false
}
}
Here's the output when running on a device:
URL = file:///var/mobile/Containers/Data/Application/1ECA9AED-6A8E-4208-A9B4-220ACE92621B/Library/Testing123/
Exists = false
fatal error: Could not create file:///var/mobile/Containers/Data/Application/1ECA9AED-6A8E-4208-A9B4-220ACE92621B/Library/Testing123/: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “Testing123” in the folder “Library”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/1ECA9AED-6A8E-4208-A9B4-220ACE92621B/Library/Testing123, NSUnderlyingError=0x1c0053800 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}: file /Users/.../AppDelegate.swift, line 30
This reproduces with two different tvOS devices, and with two different development teams / app store companies / bundle IDs.
Is anyone else having this problem? Any insights would be appreciated, thanks!
If you have any ideas about [the] preferred location to store data on the file system, please let me know.
I can certainly talk to that.
The first thing to understand here is that tvOS was designed as an always connected system, and thus apps generally don’t store user data on the Apple TV. This is pretty clearly outlined in the Local Storage for Your App Is Limited section of the App Programming Guide for tvOS.
So, the best place to store stuff on tvOS very much depends on the type of stuff you’re storing. What is that in your case?
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"