Error 256 Reading via NSString's initWithContentsOfFile - objective-c

I am attempting to read a number of files from a directory tree inside the Documents directory within my App andbox.
Most of them are xml files which I read with :
NSData *data = [NSData dataWithContentsOfFile:packagefile.
RXMLElement *rxml = [RXMLElement elementFromXMLData:data];
All but one them read OK. But one does not read and I get error code 256.
For the bad file I can also try :
NSData *data = [NSData dataWithContentsOfFile:packagefile options:0 error:&err];
but the same error code comes back . (Hence I think my issue is not related to the actual xml content of the file).
Clearing out the sandbox and copying all the files back in programatically in my App makes no difference.
What can be wrong here? What else can I do?

I have found a solution to this problem ('inexplicable error code 256'.) Hopefully this solution will be helpful to other developers who hit this issue.
It had run out of file handles.
In my App I had a also logging function which logged activity to file (a bit like Log4J). This took each logged statement – opened a file using a 'NSFileHandle' followed by 'seekToEndOfFile' followed by 'writeData'.
I was relying mistakenly upon the ARC disposing and closing the file on exit from the logging procedure. Clearly ARC does not close the file and eventually the system runs out of file handles.
The solution is simply to call 'closeFile' as one would in Java or C.

Related

Save in plist to a program directory

i need to save a .plist file NOT to documents, but to the core of program.
For example my program called "123" and if i save data, then send my app to my friend and he opens this app he could see saved data, no matter where he puts this program. I can't find solution to this problem, please help me.
I'm making mac app.
and i save plist with
[NSKeyedArchiver archiveRootObject:FBCover1.text=
[NSString stringWithFormat:#"%#",Cover1.attributedStringValue]
toFile:#"/Users/admin/FBCover1.plist"];
General answer:
If you're trying to do this on iPhone (you didn't tag this for iOS or MacOS), this isn't going to work as this will break your code signing.
If you're doing this on MacOS and you're using code signing, you'll have the same problem.
There may be places where you could save and share data, such as Game Center or DropBox or Box or some other cloud storage mechanism, but you'll need to pick up and make use of some additional API's or frameworks.
Specific answer just for you:
Instead of:
[NSKeyedArchiver archiveRootObject:FBCover1.text=[NSString stringWithFormat:#"%#",Cover1.attributedStringValue] toFile:#"/Users/admin/FBCover1.plist"];
which is big and ugly and I don't know what the heck it's doing, why not save your string this way?
NSString * stringToSave = [NSString stringWithFormat:#"%#",Cover1.attributedStringValue];
if(stringToSave)
{
NSError * error = nil;
BOOL success = [stringToSave writeToFile: #"/Users/admin/FBCovert1.txt" atomically: YES encoding: NSUTF8StringEncoding error: #error];
if(!success)
{
NSLog( #"error in saving - %#", [error localizedDescription]);
}
}
This saves the raw string into a file.
If you want to do it as a plist, then create a NSDictionary and save your string as the value with some appropriate key.
Preamble: this is an awful idea. What you should do is create a document-based application and pass your document backwards and forwards.
Literal answer:
You can use NSBundle to get the path of the resources folder within your application bundle with something like:
[[NSBundle mainBundle] resourcePath]
The resources folder is where application resources, such as plists, are meant to go. You're supposed to consider your application bundle as read-only in general but that's as good a choice as any if you want to hack away.

How to read a PGN file for parsing in objective - c

I've got a PGN file called a00.PGN in my project, which I'd like to parse to 'preload' into Core Data. Right now I'm just trying to output the file as a string with NSLog using
NSError *err = nil;
NSString *datapath = [[NSBundle mainBundle] pathForResource:#"a00" ofType:#"PGN"];
NSString *content = [NSString stringWithContentsOfFile:datapath encoding:NSASCIIStringEncoding error:&err];
NSLog(#"%#", err);
And I'm getting an error: Error Domain=NSCocoaErrorDomain Code=258 "The file name is invalid."
When I NSLog the string content it is NULL.
I have tried changing the extension to .txt but the same error is received, and have also used NSUTF8StringEncoding. What is the best way to read in this file for parsing in objective-c?
Edit 1:
To include the file in my project I dragged it from finder. I then went to my projects target and added it to the Copy Files section with Products Directory as the destination. The Subpath is empty.
Edit 2:
I've uploaded the current state of this simple project to a repo here: https://github.com/justinjdickow/PGN-Parse-into-Core-Data/
According to Technical Q&A QA1436: What is the "main bundle" of a command-line foundation tool? — actually, that document is (partially) wrong.
In a command-line tool executable, the main bundle's path would be the directory that contains the executable itself. For example, in a sample command-line tool project I made here, the following was logged to console:
mainBundle == NSBundle <~/Library/Developer/Xcode/DerivedData/mainBundle-bqmiidcdlnwumjgcnblqgtelpecl/Build/Products/Debug> (loaded),
bundlePath == ~/Library/Developer/Xcode/DerivedData/mainBundle-bqmiidcdlnwumjgcnblqgtelpecl/Build/Products/Debug
In other words, the main bundle will be your built-products directory. For NSBundle's pathForResource:ofType: to be able to find the "A00.PGN" file at runtime, that file will need to be in your built-products directory along with your executable. For example, in my case, I would need the file to be located at ~/Library/Developer/Xcode/DerivedData/mainBundle-bqmiidcdlnwumjgcnblqgtelpecl/Build/Products/Debug/A00.PGN
The easiest way to achieve that is to add a Copy Files build phase to your command line tool target. Specify the Destination as the Products Directory and include the A00.PGN file in the list of files to be copied.
Also, as others have already mentioned, NSBundle's search method is case-sensitive even if the underlying HFS+ file system on OS X is merely case-preserving. So, make sure you specify the correct filename and filename extension in your code.
Note that pathForResource:ofType: has different case-sensitivity depending whether it is iPhone simulator or iPad simulator or the device.
Make sure you are using upper/lower case consistently.

Array Doesn't load PNGs Exported from After Effects: ios, xcode

Our graphic designer is sending us .PNGs named appropiately "hide_00~iphone.png", "hide_00#2X~iphone.png" etc
He is exporting the images from after effects. I add them to the project and try to load them into an array on init. An exception is thrown each time for all of his files. Now, if I go in AND RENAME the files in the finder to exactly the same name, everything compiles fine. I have no idea what's going on here. Xcode cannot find them in the file system until I rename them. But the name is EXACTLY the same as what he sent me. I checked for white space around his file naming but everything looks fine.
Does anybody know if After Effects puts weird header info in the images? Or does this sound familiar at all to anyone. There are a whole bunch of images we are working with and I would hate to have to rename them by hand.
So I just used imageNamed and the image loaded just fine. So obviously your routine to load the image by path/name is flawed. You can post that and it can be evaluated - but this has nothing to do with 'After Effects'.
EDIT: For the record, my system is Lion Xcode 4.4.1 and my project set for iOS 5.1. I took your file from dropbox, and verified that in my project I CAN load the image as you are trying to do:
for (int i = 6; i < 7; i++) {
NSString *path = [NSString stringWithFormat:#"hide_step_seq_%02d", i];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:#"png"]];
NSLog(#"PATH %# image=%#", path, image);
}
2012-09-13 07:15:23.241 Searcher[58114:f803] PATH hide_step_seq_06 image=<UIImage: 0x6a4cb30>
So, where to go from here? I've tried to help several people here who get burned by the #2x or ~iphone suffixes. For some reason a few people cannot ever seem to get this to work - all I can think of is there is some flag deep in the system that gets toggled and there is no way to untoggle it.
My suggestion is to try using the actual complete file name - try appending ~iphone and see if that works.
You can also in Terminal do a 'ls *.png | od -c' before changing the name and afterwards, to verify that absolutely the characters are the same.
The last thought I have on this is that files have many attributes: creation time, last access, last modiied, extended attributes, permissions, etc. It is possible (while unlikely) that for some reason one of these values blocks the system from attempting to use the ~iphone suffix.
I really wish I could help you further. If you want to put a simple little project together that does nothing more than tries to open a few images and it fails, zip the whole project up, put on dropbox, I'd be more than willing to run it on my system to try and duplicate the problem. You can also do as I did in the code above and verify that path looks good and the image is nil.

Getting The XML Text Of A .xib File

I know .xib files are really .xml files (as evidenced primarily by the fact that I can rename their extension and open them in text edit and dashcode), so theoretically, it would be possible to open a xib as xml and parse it in an application. I took a look in the Build Settings for my sample app, and found that xib's aren't compiled, rather they are Copy bundle Resources. Sounds good, right? But the files are giving me (null) strings when I try and get their text with these methods:
NSString *string = [[NSBundle mainBundle]pathForResource:#"Empty" ofType:#"xib"];
NSString *xmlString = [[NSString stringWithContentsOfFile:string encoding:NSUTF8StringEncoding error:nil] retain];
NSLog(#"%#", xmlString);
[textView setText:xmlString];
It NSLog's (null) both for xmlString and for string. So I thought of trying it with another Copy Bundle Resource, so I changed string to this:
NSString *string = [[NSBundle mainBundle]pathForResource:#"InfoPlist" ofType:#"strings"];
and it logged a perfect file path! However, xmlString still logged (null).
Then, I tried getting an error code, and I got this:
The operation couldn’t be completed. (Cocoa error 258.)
It makes me think there is some extra compiling going on behind the scenes with .xib files (I read somewhere that they are turned into nibs by the compiler) that is making them unreadable or unreachable. But there are apps out that I know of (Interface being a prime example) that can read and write .xib files.
So, can anyone help me get the xml of a .xib file?
Actually xib files are compiled into nib files. Use Show Package Contents on the compiled .app and you will see that there are no xib files but nib files instead.
Edit It looks like the nib file format is a proprietary format. But I am not sure.
Form Apple documentation:
The original format uses files with extension .nib. This a proprietary, binary format for the representation of Interface Builder user interface objects and connections.
Edit 2 It looks like there is some option to prevent Xcode from compiling the xib files into nib files. But I am still not sure about how to do that, and what is the actual result.
Edit 3 I tried that on Xcode. When I turned Flatten Complied Xib files to No, I still got nib files in the binary but they are not compressed this time. Inside these nib files there is a file designtime.nib that has an xml format.

view .doc, .docx, .rtf, .ppt file in iphone using Webview

I want to view .doc, .docx, .rtf, .ppt file in iphone.
But I guess something is going wrong at my side and its not working for the above formats but my code is working fine for .txt and .pdf files.
I have the read the document regarding Webview it states it supports viewing of the above document.
below is my snippet for .doc
[webView loadData:requestData MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:nil];
for .ppt I am using MIME type as "application/vnd.ms-powerpoint"
Note: If I am making MIME type as "text/html" for .doc/.rtf then it displays some garbage data So I think there is something missing in MIME type from my side.
Any help is highly appreciated.
Waiting for your reply.
Update:........
It seems there is some issue with NSData for opening these type of file formats.
My data is encrypted so I cannot use
requestWithURL directly and other thing is I need to pass credentials to get the file and the credentials doesnt work proplerly if I save the credentials using
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
and then make the request using below
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] ]
Is there any way I can use NSData for the above formats so that I can make async call and pass my credentials and then decrypt the data and show it in WebView.
Try using a QuickLook Framework which is an amazing feature given by Apple. If you have seen this that when you select ANY FILE on MAC System and click SPACE BAR then you will a quick preview of your file it may be any thing... *.pdf, *.doc, *.docx or any thing... Even if you have not installed those softwares for same files you will get a PREVIEW for those files...
QuickLook framework also gives same functionality... Please go through some of the links for the same..
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
https://github.com/rob-brown/Demos
https://github.com/rob-brown/RBFilePreviewer
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
(Recommended..) http://iosdevelopertips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html
Not all of those formats (docx) are supported. This looks like the official word. In the example Apple does not specify a mime type.
This question suggests that you must use an NSURLRequest instead of loadData.
Edit:
As far as I have seen, you cannot directly pass data to loadData for these types.
The easy work around is to write to a temporary file. You can delete it in webViewDidFinishLoad or, technically, as soon as the the file has been opened.
The hard work around is to use NSURLCache. You should be able to implement a custom NSURLCache and have WebKit use it via setSharedURLCache. Your cache would basically know how to get your local encrypted files and pretend that they are cached. I have not tried this, but I think it is your best bet. This approach may be blocked just like loadData is.
You need to supply something to baseURL:. For example:
[webView loadData:requestData MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#"http://localhost/"]];
Works for me now.
Try it on an iPhone instead of Simulator