iOS - Can't Read File Located In Documents Directory - ios

I am trying to read a file in which I successfully downloaded and saved in the Documents directory. However, when I try to read it, if fails.
Here is the error:
2016-03-28 21:00:26.585 App[569:4103] Path is /var/mobile/Applications/3AFA2430-C0DC-44CD-95F8-A89D82B2C348/Documents/combo.bin
2016-03-28 21:00:26.603 App[569:4103] Error in reading Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x17df29d0 {NSFilePath=/var/mobile/Applications/3AFA2430-C0DC-44CD-95F8-A89D82B2C348/Documents/combo.bin, NSUnderlyingError=0x17df90b0 "The operation couldn’t be completed. Permission denied"}
Here is my code:
NSError *error;
NSData *firmwareContentData = [NSData dataWithContentsOfFile:FIRMWARE_LOCAL_PATH options:NSDataReadingMappedIfSafe error:&error];
NSLog(#"Path is %#", FIRMWARE_LOCAL_PATH);
if(error)
{
NSLog(#"Error in reading %#", error);
return;
}
local path is
#define FIRMWARE_LOCAL_PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"combo.bin"]
I tried using NSTemporaryDirectory(), using NSFileManager to get the contents of the file, but none of them works. Any idea why? Thanks in advance!

You have a permissions error. You don't have the right permissions to open the file. Wherever you got it from, you're locked out. You might try to download it in the simulator, and check it through Apple's file system to see what permissions it actually downloaded with. The path is:
~/Library/Developer/CoreSimulator/Devices//data/Containers/Data/Application//Documents.
Replace the two big random strings with the directories that show a mod date of today, or NSLog the real path from your iOS app.

Related

Issues writing files on iOS

I'm fighting with the following error NSCocoaErrorDomain 513 reported by a very small number of users:
Unable to create directory at path /var/mobile/Containers/Data/Application/EBE2C5D8-5AEC-4D62-9393-B19CAD598FE5/Documents/documents/FF2F88FB-2C07-4FA3-988E-58AD5C21F659/9A02F8A0-74EB-4ED6-81B6-4F40653856D3.
Error: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “9A02F8A0-74EB-4ED6-81B6-4F40653856D3” in the folder “FF2F88FB-2C07-4FA3-988E-58AD5C21F659”."
UserInfo={
NSFilePath=/var/mobile/Containers/Data/Application/EBE2C5D8-5AEC-4D62-9393-B19CAD598FE5/Documents/documents/FF2F88FB-2C07-4FA3-988E-58AD5C21F659/9A02F8A0-74EB-4ED6-81B6-4F40653856D3,
NSUnderlyingError=0x15e09de00 {
Error Domain=NSPOSIXErrorDomain
Code=13 "Permission denied"
}
}
This error means that the directory cannot be created because of a permission error. That's where I'm lost as the only reason I can see would be if I'm creating a file outside of my app's sandbox.
The code generating this error:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString *directory = [documentsDirectory stringByAppendingPathComponent:documentsPathAndUUIDs];
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:directory
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
NSError(#"Unable to create directory at path %#. Error: %#", directory, error);
}
A couple things worth noting:
This path isn't saved, it's regenerated every time, so it's not as if the app container had changed between installs;
The users seem to have available disk space;
This affects at least iOS 9 (I don't have enough reports to know if it also affects iOS 10)
Would anyone have a hint of why this could happen?
Do you have sudo/chown privileges on /var? If not and you're running an OS post Mavericks, you may need to restart in safety mode and disable CSRUtil
Documentation on CSRUtil

Flag "do-not'backup" files in iOS Main Bundle

I uploaded an app to Appstore but got rejected due to files being backed up to iCloud, when only user generated content should be backed up.
The thing is that I have a json in MainBundle that I use only the first time to create and update CoreData. Once I update CoreData, the json is no longer needed. I tried a solution to delete this json file but I can't since it's not permitted by Apple to delete or edit content from the Main Bundle. I, then, leave the json unused and in the Main Bundle, but the app was rejected due to this file being backed up to iCloud.
I tried this solution below, that Apple gave me to solve this problem:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(#"Error excluding %# from backup %#", [URL lastPathComponent], error);
}
return success;
}
I still can't solve the problem, because every time I try to use this code, I get this error:
NSUnderlyingError=0x15f87bb0 "The operation couldn’t be completed. Operation not permitted"
My understanding is that I can't flag the json as "do-not-backup" because I can't delete or edit any files that are in MainBundle. I tried arguing with Apple that this happens but got the same response from them.
Any ideas on how to solve this problem?

Save UIDocument to iCloud give me NSCocoaErrorDomain Code=4

in my app i want write document on iCloud, but i have a problem with one iCloud account, this is the scenario:
iCloud Account A, all works perfectly, i can write and read, with iPad and iPhone.
iCloud Account B, same as above, i can write and read.
iCloud Account C, doens't work, i can't write and read and give me the error.
This is a scratch of code:
NSURL *ubiqContainer = [[NSFileManager defaultManager]
URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiqContainer
URLByAppendingPathComponent:#"Documents"]
URLByAppendingPathComponent:kFILENAME];
MyDocument *mydoc = [[MyDocument alloc]
initWithFileURL:ubiquitousPackage];
self.mydoc = mydoc;
[mydoc saveToURL:[mydoc fileURL]
forSaveOperation:UIDocumentSaveForCreating
completionHandler:
^(BOOL success) {
//success is always FALSE for the Account C
}];
then i have implemented this method:
- (void) handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted{
NSLog(#"error: %#", [error description]);
NSLog(#"permitted: %d", userInteractionPermitted);
}
and this is the log:
error: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x15ef24f0 {NSFileNewItemLocationKey=file:///private/var/mobile/Applications/2A1F6A00-AA50-4D44-AFA1-89D0CA747EBB/tmp/(A%20Document%20Being%20Saved%20By%20My%20App)/MyApp.esd, NSFileOriginalItemLocationKey=file:///private/var/mobile/Library/Mobile%20Documents/EAYDD3QH5Q~com~mycompany~My-App/Documents/MyApp.esd, NSUnderlyingError=0x15e3c870 "The operation couldn’t be completed. (Cocoa error 4.)", NSURL=file:///private/var/mobile/Library/Mobile%20Documents/EAYDD3QH5Q~com~mycompany~My-App/Documents/MyApp.esd}
2013-10-20 16:53:24.358 My App[222:3707] permitted: 1
i can't understand why doens't work on this account, and on the other works...what is wrong? i have reinstalled all profiles in the device with the account problem, but doesn't work, any idea?
I fix the problem deleting the iCloud folder of the app, using the command go to Folder in the Finder:
Users/"username"/.Library/Mobile Documents/"app folder"
and then here i delete the folder, in this way i fix the problem.

UIDocument opening is never finished

I have an iOS 5.1 app, and I'm trying to optimize it for iOS 6. The problem is that openWithCompletionHandler: is never finished. As I see, second device can't download this file. But device-owner works with file fine.
My app uses 2 file types: the first is only text data, and the second is text with picture data. Everything is completely cool with text files. I suppose the problem is in file size, but delay (I tried 40 seconds) doesn't help for me.
I try to use dataWithContentsOfFile: instead of UIDocument and the result is
Data: (null) WithError: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"
I'm using the following code to open the document
[currentFile openWithCompletionHandler:^(BOOL success) {
NSLog(#"File Opened"); // <<--- Not processed, never reached
}];
I added the following right before opening, but it has no effect:
[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

Xcode Unit Test and Writing Data to Disk

I am trying to write logic test to debug a a method that saves image data to disk. I use the following lines to write image data to the documents directory:
docspath = [[NSString alloc] initWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) objectAtIndex:0]];
fullpath = [[NSString alloc] initWithFormat:#"%#/%#", docspath, _name];
BOOL written = [fileData writeToFile:fullpath options:NSDataWritingAtomic error:&dataerror];
Except, the data is not written and the error description returns no such file or directory. I am running this code under a test target in the iOS 5.1 simulator. Do I need to create intermediate directories to get to the documents directory, or is there another issue with writing to disk under a test target?
The error returned:
2012-06-29 16:13:31.819 otest[4597:7b03] /Users/myaccount/xcode_projects/myproject/myproject/myfolder/MMFileManager.m - 285: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x1e1e3a0 {NSUserStringVariant=Folder, NSFilePath=/Users/myaccount/Library/Application Support/iPhone Simulator/5.1/Documents/mmLogo.png, NSUnderlyingError=0x1e1b430 "The operation couldn’t be completed. No such file or directory"}
Unit test is not in your app, therefore, you can't access Documents directory in the unit test.
refer to NSHomeDirectory in iPhone unit test

Resources