Files and Storage

RSS for tag

Ask questions about file systems and block storage.

Posts under Files and Storage tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

On File System Permissions
Modern versions of macOS use a file system permission model that’s far more complex than the traditional BSD rwx model, and this post is my attempt at explaining that model. If you have a question about this, post it here on DevForums. Put your thread in the App & System Services > Core OS topic area and tag it with Files and Storage. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" On File System Permissions Modern versions of macOS have four different file system permission mechanisms: Traditional BSD permissions Access control lists (ACLs) App Sandbox Mandatory access control (MAC) The first two were introduced a long time ago and rarely trip folks up. The second two are newer, more complex, and specific to macOS, and thus are the source of some confusion. This post is my attempt to clear that up. Error Codes App Sandbox and the mandatory access control system are both implemented using macOS’s sandboxing infrastructure. When a file system operation fails, check the error to see whether it was blocked by this sandboxing infrastructure. If an operation was blocked by BSD permissions or ACLs, it fails with EACCES (Permission denied, 13). If it was blocked by something else, it’ll fail with EPERM (Operation not permitted, 1). If you’re using Foundation’s FileManager, these error are both reported as Foundation errors, for example, the NSFileReadNoPermissionError error. To recover the underlying error, get the NSUnderlyingErrorKey property from the info dictionary. App Sandbox File system access within the App Sandbox is controlled by two factors. The first is the entitlements on the main executable. There are three relevant groups of entitlements: The com.apple.security.app-sandbox entitlement enables the App Sandbox. This denies access to all file system locations except those on a built-in allowlist (things like /System) or within the app’s containers. The various “standard location” entitlements extend the sandbox to include their corresponding locations. The various “file access temporary exceptions” entitlements extend the sandbox to include the items listed in the entitlement. Collectively this is known as your static sandbox. The second factor is dynamic sandbox extensions. The system issues these extensions to your sandbox based on user behaviour. For example, if the user selects a file in the open panel, the system issues a sandbox extension to your process so that it can access that file. The type of extension is determined by the main executable’s entitlements: com.apple.security.files.user-selected.read-only results in an extension that grants read-only access. com.apple.security.files.user-selected.read-write results in an extension that grants read/write access. Note There’s currently no way to get a dynamic sandbox extension that grants executable access. For all the gory details, see this post. These dynamic sandbox extensions are tied to your process; they go away when your process terminates. To maintain persistent access to an item, use a security-scoped bookmark. See Accessing files from the macOS App Sandbox. To pass access between processes, use an implicit security scoped bookmark, that is, a bookmark that was created without an explicit security scope (no .withSecurityScope flag) and without disabling the implicit security scope (no .withoutImplicitSecurityScope flag)). If you have access to a directory — regardless of whether that’s via an entitlement or a dynamic sandbox extension — then, in general, you have access to all items in the hierarchy rooted at that directory. This does not overrule the MAC protection discussed below. For example, if the user grants you access to ~/Library, that does not give you access to ~/Library/Mail because the latter is protected by MAC. Finally, the discussion above is focused on a new sandbox, the thing you get when you launch a sandboxed app from the Finder. If a sandboxed process starts a child process, that child process inherits its sandbox from its parent. For information on what happens in that case, see the Note box in Enabling App Sandbox Inheritance. IMPORTANT The child process inherits its parent process’s sandbox regardless of whether it has the com.apple.security.inherit entitlement. That entitlement exists primarily to act as a marker for App Review. App Review requires that all main executables have the com.apple.security.app-sandbox entitlement, and that entitlements starts a new sandbox by default. Thus, any helper tool inside your app needs the com.apple.security.inherit entitlement to trigger inheritance. However, if you’re not shipping on the Mac App Store you can leave off both of these entitlement and the helper process will inherit its parent’s sandbox just fine. The same applies if you run a built-in executable, like /bin/sh, as a child process. When the App Sandbox blocks something, it typically generates a sandbox violation report. For information on how to view these reports, see Discovering and diagnosing App Sandbox violations. To learn more about the App Sandbox, see the various links in App Sandbox Resources. For information about how to embed a helper tool in a sandboxed app, see Embedding a Command-Line Tool in a Sandboxed App. Mandatory Access Control Mandatory access control (MAC) has been a feature of macOS for many releases, but it’s become a lot more prominent since macOS 10.14. There are many flavours of MAC but the ones you’re most likely to encounter are: Full Disk Access (macOS 10.14 and later) Files and Folders (macOS 10.15 and later) App container protection (macOS 14 and later) App group container protection (macOS 15 and later) Data Vaults (see below) and other internal techniques used by various macOS subsystems Mandatory access control, as the name suggests, is mandatory; it’s not an opt-in like the App Sandbox. Rather, all processes on the system, including those running as root, as subject to MAC. Data Vaults are not a third-party developer opportunity. See this post if you’re curious. In the Full Disk Access and Files and Folders cases, users grant a program a MAC privilege using System Settings > Privacy & Security. Some MAC privileges are per user (Files and Folders) and some are system wide (Full Disk Access). If you’re not sure, run this simple test: On a Mac with two users, log in as user A and enable the MAC privilege for a program. Now log in as user B. Does the program have the privilege? If a process tries to access an item restricted by MAC, the system may prompt the user to grant it access there and then. For example, if an app tries to access the desktop, you’ll see an alert like this: “AAA” would like to access files in your Desktop folder. [Don’t Allow] [OK] To customise this message, set Files and Folders properties in your Info.plist. This system only displays this alert once. It remembers the user’s initial choice and returns the same result thereafter. This relies on your code having a stable code signing identity. If your code is unsigned, or signed ad hoc (“Signed to Run Locally” in Xcode parlance), the system can’t tell that version N+1 of your code is the same as version N, and thus you’ll encounter excessive prompts. Note For information about how that works, see TN3127 Inside Code Signing: Requirements. The Files and Folders prompts only show up if the process is running in a GUI login session. If not, the operation is allowed or denied based on existing information. If there’s no existing information, the operation is denied by default. For more information about app and app group container protection, see the links in Trusted Execution Resources. For more information about app groups in general, see App Groups: macOS vs iOS: Fight! On managed systems the site admin can use the com.apple.TCC.configuration-profile-policy payload to assign MAC privileges. For testing purposes you can reset parts of TCC using the tccutil command-line tool. For general information about that tool, see its man page. For a list of TCC service names, see the posts on this thread. Note TCC stands for transparency, consent, and control. It’s the subsystem within macOS that manages most of the privileges visible in System Settings > Privacy & Security. TCC has no API surface, but you see its name in various places, including the above-mentioned configuration profile payload and command-line tool, and the name of its accompanying daemon, tccd. While tccutil is an easy way to do basic TCC testing, the most reliable way to test TCC is in a VM, restoring to a fresh snapshot between each test. If you want to try this out, crib ideas from Testing a Notarised Product. The MAC privilege mechanism is heavily dependent on the concept of responsible code. For example, if an app contains a helper tool and the helper tool triggers a MAC prompt, we want: The app’s name and usage description to appear in the alert. The user’s decision to be recorded for the whole app, not that specific helper tool. That decision to show up in System Settings under the app’s name. For this to work the system must be able to tell that the app is the responsible code for the helper tool. The system has various heuristics to determine this and it works reasonably well in most cases. However, it’s possible to break this link. I haven’t fully research this but my experience is that this most often breaks when the child process does something ‘odd’ to break the link, such as trying to daemonise itself. If you’re building a launchd daemon or agent and you find that it’s not correctly attributed to your app, add the AssociatedBundleIdentifiers property to your launchd property list. See the launchd.plist man page for the details. Scripting MAC presents some serious challenges for scripting because scripts are run by interpreters and the system can’t distinguish file system operations done by the interpreter from those done by the script. For example, if you have a script that needs to manipulate files on your desktop, you wouldn’t want to give the interpreter that privilege because then any script could do that. The easiest solution to this problem is to package your script as a standalone program that MAC can use for its tracking. This may be easy or hard depending on the specific scripting environment. For example, AppleScript makes it easy to export a script as a signed app, but that’s not true for shell scripts. TCC and Main Executables TCC expects its bundled clients — apps, app extensions, and so on — to use a native main executable. That is, it expects the CFBundleExecutable property to be the name of a Mach-O executable. If your product uses a script as its main executable, you’re likely to encounter TCC problems. To resolve these, switch to using a Mach-O executable. For an example of how you might do that, see this post. Revision History 2024-11-08 Added info about app group container protection. Clarified that Data Vaults are just one example of the techniques used internally by macOS. Made other editorial changes. 2023-06-13 Replaced two obsolete links with links to shiny new official documentation: Accessing files from the macOS App Sandbox and Discovering and diagnosing App Sandbox violations. Added a short discussion of app container protection and a link to WWDC 2023 Session 10053 What’s new in privacy. 2023-04-07 Added a link to my post about executable permissions. Fixed a broken link. 2023-02-10 In TCC and Main Executables, added a link to my native trampoline code. Introduced the concept of an implicit security scoped bookmark. Introduced AssociatedBundleIdentifiers. Made other minor editorial changes. 2022-04-26 Added an explanation of the TCC initialism. Added a link to Viewing Sandbox Violation Reports.  Added the TCC and Main Executables section. Made significant editorial changes. 2022-01-10 Added a discussion of the file system hierarchy. 2021-04-26 First posted.
0
0
8.6k
Nov ’24
Files and Storage Resources
General: DevForums tags: Files and Storage, Finder Sync, File Provider, Disk Arbitration, APFS File System Programming Guide On File System Permissions DevForums post File Provider framework Finder Sync framework App Extension Programming Guide > App Extension Types > Finder Sync Disk Arbitration Programming Guide Mass Storage Device Driver Programming Guide Device File Access Guide for Storage Devices Apple File System Guide TN1150 HFS Plus Volume Format Extended Attributes and Zip Archives File system changes introduced in iOS 17 DevForums post Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
0
0
1.8k
Jan ’24
File system changes introduced in iOS 17
File system changes introduced in iOS 17 As part of iOS 17, tvOS 17, and watchOS 10, the system has reorganized where applications and their data containers are stored. In previous systems, both lived within the same volume but, starting in iOS 17, they will be stored on different volumes. What does this mean for you? Copying large amounts of data from the app bundle to a data container will take longer than in previous versions of iOS. Previously that copy would have occurred as an APFS file clone, but now the operation will occur as a standard copy, which may take much significantly longer. Because the data will need to be fully duplicated, storage usage will increase more than was the case in previous versions. You should minimize the data they copy out of their app bundle and avoid any unnecessary duplication of data between the app bundle and data container. When upgrading from previous system version, splitting the data into separate volumes may mean that there is insufficient space for all existing apps and their data. If this occurs, the app's data container will remain on the device, preserving the user's data, while the app bundle itself is removed using the same mechanism as "Offload Unused Apps". The user can then restore the app once they've freed sufficient space for the app to install. Revision History 2023-07-11 First posted
0
0
2.5k
Jul ’23
Sdk access to filemanager documents directory
Not sure if my question is weird or not, but I didnt find any documentation about an sdks access to an apps documents directory. Im assuming that because sdk is part of the sandbox that it can access and read from the documents directory? i.e. if i used NSFileManager.defaultManager.URLForDirectory( directory = NSCachesDirectory, inDomain = NSUserDomainMask, appropriateForURL = null, create = false, error = null, ) to write a file called "file.txt", then any sdk my app used has the ability to access the contents of said directory? If yes, why isnt this considered something more worth mentioning? usually in filemanager tutorials they never warn about this. FYI: Im asking more for iOS and iPadOs then MacOs.
8
0
123
1h
Xcode Git integration destroyed my git local repository
This is using Xcode 16.2 (16C5032a), MacOS 15.2, on MacbookPro 16 M1 Pro. I have two repositories, one for an application and another one for a framework. The repository framework is integrated into the application one as git submodule. I was going to push the submodule to Github using Xcode git integration. It detects some conflicts and ask to pull. I pull. A window appears with some conflicting files (5 or 6). Review them. While reviewing something in Xcode crashed, but as Xcode was still up and running, ignored the crash. Decided to cancel the conflicts window and inspect my local files. Pull again but this time I decided to pull both repos. Review the conflicting changes and decided to cancel again to continue by hand in a terminal. Then, I notice that the project for the submodule is in red and that the "Changes" tab shows a bunch of files with and admiration mark. I go to the submodule folder in Finder and... everything inside the submodule folder was gone, disappeared, lost... everything, even the ".git" folder (only remained a binary folder for a component I use for the framework but is empty). I decided to run a recovery tool. It finds nothing to recover inside that folder, nothing. A lot of files even from years ago but nothing inside that folder? how is that possible? I filled a Feedback assistant issue: FB16307182 Could it be possible that Xcode "moved" everything to another place (I'm desperate so I'm starting to think in absurd explanations) Thanks
1
0
178
1w
fileImporter modifier: Problem to get a positive App Store Connect Feedback
I have written an App with a fileIpmorter modifier. I used TestFlight to pass it to friend for a test. The App works fine until my friend uses the fileImporter function: He can select the file to import (simple csv file), the fileImporter closes without having read out the csv file! I was able to analyze that the do-catch-block within the fileImporter modifier throws an error at the mac of my friend. The App behaves totally different to my Mac: On my Mac it works perfectly: the csv file is read out and the data can be used within the APP. Why does it behave like this? Why does this happen to the mac of my friend but not on mine? More strange: The Apple team is not able to even use the csv-file I passed them in the App Store Connect process: They are not able to select the file in the downloads folder. All three used MAC´s with three different behaviors? - How comes?
1
0
133
1w
Accessing/scanning the iOS Downloads folder (the one airdropped-to) from an app
Here's the problem I'm trying to solve: Create an iOS app which can scan the Downloads folder (where airdropped audio files arrive), identify audio media files, and play them, retaining some of its own metadata about them (basically, create textual notes mapped to timestamps and store that information in the apps own storage). I am not able to access that folder. I am able to get a path from NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.downloadsDirectory, FileManager.SearchPathDomainMask(arrayLiteral: FileManager.SearchPathDomainMask.userDomainMask), true) or a URL from NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.downloadsDirectory, FileManager.SearchPathDomainMask(arrayLiteral: FileManager.SearchPathDomainMask.userDomainMask), true) but let fileUrls = try fileManager.contentsOfDirectory(at:downloads, includingPropertiesForKeys: []) fails with an error that the folder does not actually exist, with or without a call to downloadsUrl.startAccessingSecurityScopedResource(). Determining whether this is a permissions issue, or if I'm getting a URL to an application-container local folder that has nothing to do with the one I am looking for is compounded by the fact that if I set the build setting Enable App Sandbox, then deployment to my phone fails with Failed to verify code signature. I have spent hours trying every possible combination of certificates and deployment profiles, and ensured that every possibly relevant certificate is trusted on my phone. Disable app-sandbox and it deploys fine, either with automatic signing or an explicit cert and profile. I have an entitlements file with the following - though, without the ability to enable app sandbox and run it on a phone with actual contents in the downloads folder, it is probably not affecting anything: <key>com.apple.security.files.downloads.read-only</key> <true/> <key>com.apple.security.files.user-selected.read-only</key> <true/> <key>com.apple.security.app-sandbox</key> <true/> So, questions: Should the URL returned by the above call be the Downloads/ folder airdropped to in the first place? Or is it a URL to some app-local folder that does not exist? Does the entitlement com.apple.security.files.downloads.read-only even allow an app to list all files in the downloads directory (presumably asking the user's permission the first time), or does the permission only get requested when using a picker dialog? (the point here is to find any new audio files without making the user jump through hoops) If I could get it deployed with app-sandbox enabled, would the above code work? Backstory: I'm a software engineer, audio plugin author, Logic Pro user and musician. My workflow (and probably many other Logic user's) for work-in-progress music is to airdrop a mix to my phone, listen to it in a variety of places, make notes about what to change, edit - rinse and repeat. For years I used VLC for iOS to keep and play these in-progress mixes - you could airdrop and select VLC as the destination (yes, Logic can add to your Apple Music library, but trust me, you do not want 20 revisions of the same song cluttering your music library and sync'd to all your devices). Last year, the behavior of Airdrop changed so that the target app for audio is always Files, period, wrecking that workflow. While I eventually discovered that, with an elaborate and non-obvious dance of steps, it is possible to copy files into VLC's folders, and make them available that way, it is inconvenient, to say the least - and VLC is less than fabulous anyway - it would be nice to have an app that could associate to-do notes with specific timestamps in a tune, A/B compare sections between old and new versions and things like that. So, figuring sooner or later I was going to get into a car accident futzing with the Files app to listen to mixes while driving, perhaps I should write that app. But the ability to do that at all relies on the ability of an app to list and access the Downloads folder airdropped audio files land in (assuming the user has given permission to access it, but that should be needed once).
4
0
192
1w
Download Rendered PDF
I need to render a PDF and then make it downloadable to the mobile device. I can generate the PDF but I'm unsure how to configure the download aspect. I have the following code: let renderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: 612, height: 792)) let pdf = renderer.pdfData { (context) in context.beginPage() let text = "Test" as NSString text.draw(in: CGRect(x: 0, y: 0, width: 100, height: 25))
4
0
298
7h
volumeTotalCapacityKey value is lower than total storage in iOS Settings
Hi, I develop a feature to get the iPhone's total storage. After some researching, the way I can get the total storage of iPhone is using this code. class DiskStatus { /// Helper method to query against a resource value key private static func getVolumeResourceValues(for key: URLResourceKey) -> URLResourceValues? { let fileUrl = URL(fileURLWithPath: "/") let results = try? fileUrl.resourceValues(forKeys: [key]) return results } /// Volume’s total capacity in bytes. public static var totalCapacity: Int? { get { let resourceValues = getVolumeResourceValues(for: .volumeTotalCapacityKey) return resourceValues?.volumeTotalCapacity } } } When I print the totalCapacity, its value is 254807724032 bytes. If I convert it to GB using decimal system it will be 254.8GB. When I looked into Settings, the total storage of my iPhone is 256GB. My questions are: Why the total storage shown in Settings different with my code result? How to achieve so that I can show exact value in Settings? Thank you.
1
0
142
2w
iOS sdcard plug-in/unplug event and read problems
Currently, we use UIDocumentPickerViewController to read the user's connected sdcard. However, this requires the user to select manually, and the event cannot be monitored. But recently I found that ImageCaptureCore's ICDeviceBrowserDelegate can monitor the plug-in and unplug events in the didAddDevice callback, but cannot obtain the path of the storage card. I wonder if there is a way to combine the advantages of both, so that with the user's consent, the card reader events can be monitored and the content can be read directly without the user manually selecting the folder
0
0
123
2w
Regarding the use of external storage USB-C within the application, after inserting the USB-C storage device into IOS, the app program can directly read and write files
Hello! We currently require the development of an iOS system for encrypting and authorizing photos, videos, voice memos, or other files stored on our devices to a connected USB-C storage. The encrypted files can be accessed through authorization. We have already encrypted and authorized the files to be stored on the app's mobile storage, and cannot directly store them to USB-C (this requirement is based on the Apple camera RroRes, which uses external storage for direct storage). We are seeking technical support from Apple.
1
0
224
2w
AppleDouble, aka dotbar, files not removed via Finder
I've having trouble deleting AppleDouble files residing on my custom filesystem through Finder. This also affects files that use the AppleDouble naming convention, i.e. their names start with '._', but aren't AppleDoubles themselves. dtrace output In vnop_readdir, 'struct dent/dentry' is set up for dotbar files and written to the uio_t buffer. It's just that my vnop_remove is never called for dotbar files, and I don't understand why not. Dotbar files are removed successfully, when deleted through command line. For SMBClients, vnop_readdir is followed by vnop_access, followed by vnop_lookup, followed by vnop_remove of dotbar files. SMBClient rm dotbar files dtrace output Implementing vnop_access for my filesystem did not result in the combination of vnop_lookup and vnop_remove being called for dotbar files. Perusing the kernel sources, I observed the following functions that might be involved, but I have not way of verifying this, as none of the functions of interest are dtrace(1)-able, rmdir_remove_orphaned_appleDouble() in particular. rmdir_remove_orphaned_appleDouble() -> VNOP_READDIR(). rmdirat_internal() -> rmdir_remove_orphaned_appleDouble() unlinkat()-> rmdirat_internal() rmdir()-> rmdirat_internal() Any pointers on how dotbar files may be removed through Finder would be greatly appreciated.
1
0
247
2w
SwiftUI FileDocument: Modify the default save dialog
Is it possible to change the default save dialog that appears when creating a document based MacOS app in SwiftUI? I have a basic FileDocument struct that gets called to a view using a DocumentGroup scene. struct MyFile: FileDocument { static let readableContentTypes: [UTType] = [.myFileType] static let writeableContentTypes: [UTType] = [.myFileType] var list: [String] init(configuration: ReadConfiguration) throws { let data = configuration.file.regularFileContents! let JSONDecoder = JSONDecoder() do { try list = JSONDecoder.decode([String].self, from: data) } catch { throw CocoaError(.fileReadCorruptFile) } } func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { let JSONEncoder = JSONEncoder() JSONEncoder.outputFormatting = .prettyPrinted do { data = try JSONEncoder.encode(self.list) } catch { print(error.localizedDescription) throw CocoaError(.fileWriteUnknown) } return .init(regularFileWithContents: data) } } This gets called at the DocumentGroup DocumentGroup(newDocument: MyFile(), editor: { document in ContentView(document: document.$document) }) But when I save the file, I want the save dialog that appears to have something like a 'Tags' textField that can also store information about the file. Something similar to this: (https://i.sstatic.net/AJQ3YNb8.png) From what I can find, there isn't much information about this other than manually creating an NSSavePanel class and overriding the current save function
0
0
190
4w
caches directory counts towards app storage?
throughout all of Foundation's URL documentation, its called out in multiple places that data stored inside an app sandox's caches directory doesn't count towards data and documents usage in the settings app but in practice, it looks like storing data there does in fact count towards documents & data for the app i'm trying to understand if the docs are wrong, if theres a bug in the settings app, or if this is a mistake on my part
3
0
336
Dec ’24
you don't have permission to view it
there are other unresolved posts on this issue but maybe mine is solvable. I copied the files I contributed from a working project to a new project using the finder (drag and drop). So I did not copy entitlements. Xcode automatically added the files to the project just fine. My app uses data on a removable drive, or used to, because I now get the error above. At one point I copied the product app to the desktop hoping to get the permissions alert window, but it never came. Oddly, in System Settings/Privacy & Security/Files & Folders my "new" app is listed with "Removable Volumes" slider = on. The file permissions (unix-like) on that drive haven't changed between projects and allow everyone to read. How can I fix this?
3
0
252
4w
st_dev of mount point directory is different to device ID of device-file
I have NTFS which is mounted on '/Volumes/usb_vol' #mount Filesystem Mounted on /dev/disk5s1 /Volumes/usb_vol The following simple code reports different values of device Id for device-file and mount point directory struct stat buf; for (int i = 1; i < argc; i++) { std::cout << argv[i] << std::endl; if (stat(argv[i], &buf) < 0) { continue; } if (S_ISBLK(buf.st_mode)) { std::cout << "st_rdev (" << major(buf.st_rdev) << "/" << minor(buf.st_rdev) << ") hex: " << std::hex << buf.st_rdev << std::endl; } else { std::cout << "st_dev (" << major(buf.st_dev) << "/" << minor(buf.st_dev) << ") hex: " << std::hex << buf.st_dev << std::endl; } } Output: /dev/disk5s1 st_rdev (1/22) hex: 1000016 /Volumes/usb_vol st_dev (48/119) hex: 30000077 I believe this is expected but I have not found any explanation of this behaviour. Are there any explanation of difference these values? I can assume the stat() will report (48/119) for all objects which are located on this file system. Is it correct? Thank you for the help!
7
0
247
1w
Xcode SwiftUI Preview "app" wants to access data from other app
I am writing SwiftData app, using a group container. When editing a SwiftUI file, every couple of seconds a dialog - that the app wants to access data from other apps - pops up. It is impossible to edit a view file while Canvas preview is open. If preview is resumed the dialog has to be confirmed twice. Each time the app is started from Xcode, the dialog has to be confirmed again. Any idea, how to stop these boring dialogs?
0
0
205
Dec ’24
User-Generated Files in Documents Directory Deleted After App Restart on iOS 18.2 (iPhone 11)
Hello, I am encountering an issue with user-generated files stored in the Documents directory on an iPhone 11 running iOS 18.2. The problem occurs as follows: 1.The app generates and saves files in the Documents directory using FileManager. 2.These files are successfully saved and remain accessible while the app is running. 3.After restarting the app, the files appear to have been deleted from the Documents directory. I have confirmed that: 1.The files are being saved to the correct location (Documents directory) and can be accessed during the current app session. 2.The app is not explicitly deleting these files during shutdown or restart. 3.This behavior is consistent across multiple app restarts.
1
0
189
Dec ’24
User-Generated Files in Documents Directory Deleted After App Restart on iOS 18.2 (iPhone 11)
Hello, I am encountering an issue with user-generated files stored in the Documents directory on an iPhone 11 running iOS 18.2. The problem occurs as follows: 1.The app generates and saves files in the Documents directory using FileManager. 2.These files are successfully saved and remain accessible while the app is running. 3.After restarting the app, the files appear to have been deleted from the Documents directory. I have confirmed that: 1.The files are being saved to the correct location (Documents directory) and can be accessed during the current app session and from iExplorer. 2.The app is not explicitly deleting these files during shutdown or restart. 3.This behavior is consistent across multiple app restarts.
0
0
130
Dec ’24
AppGroup intermittent permission errors
I recently started saving a file, where I hold some app state, to an AppGroup in order to share it with my widget extension. Of the some 16k daily active users of my app, 55 are initially unable to read the file (it might not yet be created). And they are unable to write a file to the AppGroup as well. I only know this due to logging the error to Crashlytics. Error Reading: "The file “BFTPreferences” couldn’t be opened because you don’t have permission to view it. Error Code:257" My App sees this error and informs the user to restart their device. I have not been contacted by any of these users for support, so I assume the restart fixes things. Has anyone seen this before? Is restarting the device the only fix? Am I doing something wrong? The only solution I can think of currently is to save the file to the app's Documents directory and save a copy to the AppGroup for use in my extensions. At least then the main app won't have an issue and it will just be the widget unable to display data until a restart. Reading the file: do { // archive data let data = try PropertyListSerialization.data(fromPropertyList: preferences, format: .xml, options: 0) // write data do { if let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) { let fullPath = groupURL.appendingPathComponent(preferencesFileName) try data.write(to: fullPath) } else { fatalError("Unable to find app group \(appGroupIdentifier)") } } catch { logthis("Failed to write dictionary data to disk. \(error.localizedDescription)") } } catch { logthis("Failed to archive dictionary. \(error.localizedDescription)") } Writing the file: do { if let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) { let fullPath = groupURL.appendingPathComponent(preferencesFileName) let data = try Data(contentsOf: fullPath) if let dictionary = try PropertyListSerialization.propertyList(from: data, format: nil) as? NSMutableDictionary { preferences = dictionary } } else { fatalError("Unable to find app group \(appGroupIdentifier)") } } catch { if (error as NSError).code == NSFileReadNoSuchFileError { // file doesn't exist so create it } else { logthis("Couldn't read BFTPreferences:\(error.localizedDescription)\nError Code:\((error as NSError).code)") <--error code 257 is caught here } }
0
0
151
Dec ’24