Xcode 15's "Replace Container" feature replaces the container with incorrect permissions?

I recently updated to Xcode 15.0 and seem to be encountering permissions related errors after using the Replace Container function to replace an app container. Similarly, editing the current run scheme and choosing a new App Data container fails with an identical error.

It seems like the Documents directory and other similar directories are blocked from being able to write. Here is an example of what appears in the log after replacing the container:

Failed to create directory /var/mobile/Containers/Data/Application/A3C32E7A-34F3-4C69-B037-478027F2A4AC/Library/Preferences because of 13.
Couldn't write values for keys (
    "/google/measurement/app_instance_id"
) in CFPrefsPlistSource<0x280a82760> (Domain: com.google.gmp.measurement, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): Directory needed
os_unix.c:47395: (13) lstat(/private/var/mobile/Containers/Data/Application/A3C32E7A-34F3-4C69-B037-478027F2A4AC/Documents)
- Permission denied os_unix.c:47395: (13) lstat(/private/var/mobile/Containers/Data/Application/A3C32E7A-34F3-4C69-B037-478027F2A4AC/Documents/<redacted>.sqlite)
- Permission denied os_unix.c:46898: (13) statfs(/private/var/mobile/Containers/Data/Application/A3C32E7A-34F3-4C69-B037-478027F2A4AC/Documents/<redacted>.sqlite)
- Permission denied

Steps to reproduce:

  1. Create a new app and replace -viewDidLoad: with the following. This will write a string to a new file and read that string:
NSFileManager * const fileManager = [NSFileManager defaultManager];
NSURL * const documentsDirectoryURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
if (!documentsDirectoryURL) {
    [NSException raise:@"DocumentsDirectoryException" format:@"Application documents directory URL is nil"];
}
NSURL * const fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"test.txt"];
if ([fileManager fileExistsAtPath:[fileURL path]]) {
    NSError *error;
    if (![[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error]) {
        [NSException raise:@"DocumentsDirectoryException" format:@"Encountered error removing file: %@",error];
    }
}

NSString * const fileContents = [NSString stringWithFormat:@"Date: %@",[NSDate date]];
NSError *error;
if (![[fileContents dataUsingEncoding:NSUTF8StringEncoding] writeToURL:fileURL options:0 error:&error]) {
    [NSException raise:@"DocumentsDirectoryException" format:@"Encountered error writing to file: %@",error];
}

NSString * const writtenFileContents = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:fileURL] encoding:NSUTF8StringEncoding];
NSLog(@"Written file contents: %@", writtenFileContents);
  1. Run the app and observe the log message "Written file contents: ..."
  2. Save the app container via Window > Devices & Simulators by clicking the app, clicking the "(...)" button, and then clicking "Download Container..."
  3. Use the same procedure in step 3 but instead, click "Replace Container..." and choose the existing app container that was downloaded
  4. Run the app again and observe that it fails with the following exception message:
*** Terminating app due to uncaught exception 'DocumentsDirectoryException', reason: 'Encountered error writing to file: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “test.txt” in the folder “Documents”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/CF1E09C8-7BE2-4D87-8AD2-648AFBE038A5/Documents/test.txt, NSUnderlyingError=0x2831d4d50 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}'

Is anyone else encountering this issue and can confirm? Are my next steps to create a bug report? Is there a manner to escalating the bug report? This is a pretty core piece of functionality for QA and testing apps.

Post not yet marked as solved Up vote post of awojnowski Down vote post of awojnowski
5.1k views

Replies

I can confirm it. It seems to be an iOS related issue. I restored the device with iOS 16. The replacement of the container works there again, even with Xcode 15.

I am having the same problem. Looks like I'm completely hosed, since I updated my Mac to Sonoma, I can't run Xcode 14 to fix the problem!

  • I updated my iPad to iOS 17.0.3 thinking maybe Apple was on top of this and that this would fix it. No such luck. All I get is a different error. I'm still hosed.

    BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F69182D8-2930-4882-A8F6-D76179F1E87B.sb-857f9996-cxGWIo/Documents/MY_DB_File.sqlite invalidated open fd: 3 (0x11)

Add a Comment

Same problem here with replacing containers

Posted to Apple's Feedback Assistant with ID FB13253099

I implemented a feature to replace files in the app's sandbox using a relatively complex mechanism. This requires coordination with an HTTP server (both local and remote are feasible). When the app starts, it downloads the corresponding app files from the file server and replaces them. Honestly, it was exhausting to implement this. I hope Apple fixes this bug soon.

Http server-client sync Solution

Still occurring on Xcode 15.0.1 and iOS 17.0.3. No reply on the Feedback Assistant report.

hi I found the same error in replace container with permission denied. Did you find the resulation ?

It looks like this is resolved in Xcode 15.1, according to the Xcode 15.1 beta 3 release notes!

The solution in Xcode 15.1 beta 3 does not work for me. The issue still exists.

This problem is not repaired in Xcode 15.1 beta 3!

This is a serious error that can be easily provoked: in the "Window" menu, select "Device and Simulators", "Download Container..." and immediately "Replace Container..." with the one you just downloaded. When the app tries to access the file in ".../Library/Application Support", the following error message appears:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=513 "Du hast nicht die Zugriffsrechte, um die Datei „Application Support“ im Ordner „Library“ zu sichern." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/F2ACE0F7-9446-46B7-8B82-AD269E62C2A1/Library/Application Support, NSUnderlyingError=0x2812861c0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}

Unfortunately this problem prevents us from testing and further developing our app.

Confirming this still fails on Xcode 15.1 beta 3 and iOS 17.0.3 with the same sample code in the original post above.

I'll update the Feedback Assistant report.

Same issue, Xcode 15.1 beta 3 and iOS 17.1.1 - 3 apps broken, urgently need a fix.

Post not yet marked as solved Up vote reply of Fyel Down vote reply of Fyel

I'm also having the same problem.

The combination of XCode15.x and iPadOS17 is NG.

The combination of XCode15.x and iPadOS16 is OK.

The combination of XCode14.x and iPadOS17 is OK.

So this is a serious problem with XCode15. I hope they fix it soon. It is not fixed in XCode15.1beta3.

Having the same problem. Reported it in FB13430457.

For those of you testing with the Xcode 15.1 beta, when you did your test did you first delete the app and its container? I'm wondering if once the container has the incorrect permissions set, that persists even when using a fixed version of Xcode. I'm hoping that if you deleted the app and its data altogether and then reinstalled it, that would work...

I'm also having problem with "Replace Container" on real device. Even when I tried to create new iOS application to test it, when I replace the container, the application crashes.

Easy way to replicate problem:

Step 1:

  • In Xcode, using project template, create new iOS application with CoreData support.

Step 2:

  • Build application and run it on real device with iOS 17.

Step 3:

  • Download Container for this application using Xcode. {menu - Window - Devices and Simulator).

Step 4:

  • Replace Container (the same container previously downloaded) using Xcode {menu - Window - Devices and Simulator).

Step 5:

  • Run application again on real device = crash/error.

Errors:

  • error: Failed to create directory: NSCocoaErrorDomain (513)
  • CoreData: Unhandled exception finding default Directory URL '+[NSPersistentContainer defaultDirectoryURL] Could not conjure up a useful location for writing persistent stores.'

My system configuration for development:

  • Xcode 15.0.1 (also tested with Xcode 15.1 beta 3 with same error)
  • iOS 17.1.2, iPadOS 17.1.2 (real devices iPhone, iPad)