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!