Xcode Unit Test and Writing Data to Disk - ios

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

Related

iOS - Can't Read File Located In Documents Directory

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.

iCloud+CoreData - "Ubiquity: Librarian" error log

I'm using iCloud+CoreData as a persistent stack for my app.
The issue comes out when my app is being installed on the second device.
I see the error log during the on-boarding phase and the error seems to affect both devices.
During the on-boarding I perform the "addPersistentStoreWithType" on my NSPersistentStoreCoordinator that triggers the iCloud Initial Seed download (if exists).
The "options" parameter for the iCloud store are the following:
NSDictionary *options =#{NSMigratePersistentStoresAutomaticallyOption: #YES,
NSInferMappingModelAutomaticallyOption: #YES,
NSPersistentStoreUbiquitousContentNameKey: "PersistentStore_iCloud",
NSSQLitePragmasOption: #{#"journal_mode" : #"DELETE"}};
The "URL" parameter used in the "addPersistentStoreWithType" method is the following:
NSURL *docUrl = [[[NSFileManager defaultManager]
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask]lastObject];
NSURL *cloudStore = [[docUrl URLByAppendingPathComponent:"PersistentStore_iCloud"]
URLByAppendingPathExtension:#"sqlite"];
After that I see the following error in the XCode logged for both devices (on the first device appears a bit later):
2015-07-03 11:14:37.362 Quadro[4110:1837886]
__45-[PFUbiquityFilePresenter processPendingURLs]_block_invoke(439): CoreData: Ubiquity: Librarian returned a serious error for starting
downloads Error Domain=BRCloudDocsErrorDomain Code=5 "The operation
couldn’t be completed. (BRCloudDocsErrorDomain error 5 - No document
at URL)" UserInfo=0x171276940 {NSDescription=No document at URL,
NSFilePath=/var/mobile/Library/Mobile
Documents/iCloud~me~actions~Quadro/CoreData/PersistentStore_iCloud/.baseline/PersistentStore_iCloud/Le0RVMmU~RX4wGSl7Dd6sM81tBZId1iVh~Wp_~6U~4U=/baseline.zip,
NSUnderlyingError=0x171258600 "The operation couldn’t be completed. No
such file or directory"} with userInfo {
NSDescription = "No document at URL";
NSFilePath = "/var/mobile/Library/Mobile Documents/iCloud~me~actions~Quadro/CoreData/PersistentStore_iCloud/.baseline/PersistentStore_iCloud/Le0RVMmU~RX4wGSl7Dd6sM81tBZId1iVh~Wp_~6U~4U=/baseline.zip";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be completed. No such file or directory\"
UserInfo=0x171278800 {NSDescription=No such file or directory}"; } for
these urls: (
"file:///var/mobile/Library/Mobile%20Documents/iCloud~me~actions~Quadro/CoreData/PersistentStore_iCloud/.baseline/PersistentStore_iCloud/Le0RVMmU~RX4wGSl7Dd6sM81tBZId1iVh~Wp_~6U~4U=/baseline.zip"
)
Conclusion
Sometimes, when the error comes out, Data is not synced completely, sometimes yes.
Both devices use a really reliable WiFi
I noticed that the system is trying to retrieve the "baseline.zip” using this directory:
"/var/mobile/Library/Mobile
Documents/iCloud~me~actions~Quadro/CoreData/PersistentStore_iCloud/.baseline/PersistentStore_iCloud/38Bj4gxLeIZNVMjIuPeKljVslTu~kP0UhpAvrkvKXOM=/baseline.zip"
Browsing the device file system folders (with iExplorer or similar utility tool) I found out that this directory does not exist in the device file system.
I use iOS Version 8.3 (12F69) with an Apple iPad mini 2 (A1489) and an iPhone6 (A1586).
--
Any suggestion would be appreciated, I'm struggling on this.
Thanks.

Getting Error Domain=NSCocoaErrorDomain Code=261 with simple case

I have the following bit of code. path is a bundle path to a file in my bundle.
_dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSError *error;
NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
What I don't understand is the _dictionary variable looks fine. It was able to get the contents and convert it to a dictionary. However, the simple case of converting the file to a string is giving me an error:
$2 = 0x12eb3d50 Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x12eb3550 {NSFilePath=/Users/ben/Library/Application Support/iPhone Simulator/6.1/Applications/82CBC7A6-2C4C-4330-9B29-7940C961B5B7/FMA.app/Licenses.plist, NSStringEncoding=4}
How can I read the PLIST file as a simple XML string? I've checked the encoding and the file is UTF-8 encoded (as is also indicated by the XML content headers). If I change the encoding to something else, I don't get an error, but the string is garbled! Any ideas?
If Xcode is copying the plist file into your app bundle then you should ensure it is copied as an XML file and not as binary (which seems to be the default setting now).
In Xcode, go to your project, select the target, go to build settings, search for 'property', change the drop-down setting for 'Property List Output Encoding' to XML.

How to create a NSString from csv file of around 2mb in size?

I have a csv file of around 2.2 mb data present in the app bundle. I am trying to store the content of this csv file in a NSString variable. The code is used for that is:
NSString *strBundle = [[NSBundle mainBundle] pathForResource:#"large" ofType:#"csv"];
NSError *error = nil;
NSString *dataStr = [NSString stringWithContentsOfFile:strBundle
encoding:NSUTF8StringEncoding
error:&error];
However, the above code deosn't store the text of the file in dataStr variable. Instead, I get the following error message:
Error=Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x1fdab580 {NSFilePath=/var/mobile/Applications/0C68E393-FBBE-45F6-819E-336D31C78043/DemoApp.app/large.csv, NSStringEncoding=4}
If, I delete content of the file such that the file is relative smaller than 2.2 mb, the above code works fine and get the string value in dataStr variable. Can some one tell me please why is this happening. Is there any limit on the amount of data that can be stored in NSString variable. How do I store the data of CSV file then?
NSCocoaErrorDomain Code=261 is: NSFileReadInapplicableStringEncodingError.
Read error because the string encoding was not applicable.
Access the bad encoding from the userInfo dictionary using the NSStringEncodingErrorKey key.
That would indicate the file is not NSUTF8StringEncoding.
You can use:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
and it will return the encoding or an error.
An NSString object initialized by reading data from the file named by
path. The returned object may be different from the original receiver.
If the file can’t be opened or there is an encoding error, returns
nil.
Apple Docs

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];

Resources