Read string from a file return nil - iphone

I try to read string from a file with :
NSString* path = [[NSBundle mainBundle] pathForResource:#"dirty" ofType:#"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
And the string i get is nil. in the error i get :
Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x21063410 {NSFilePath=/var/mobile/Applications/78889F89-B83C-480C-A246-999152EE757C/Myapp.app/dirty.txt, NSStringEncoding=4}
Any idea what is the problem?

As mentioned, Cocoa error 261 is NSFileReadInapplicableStringEncodingError which means the encoding of file is different than what you are passing in the API.
For Hebrew language encoding, try this answer
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew) error:&err];
Hope that helps!

The error seems to be the Encoding Issue ..
If you don't know the encoding of your file, you could try this method:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
Refer HERE
And
Try Something like this:
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];

Try this.
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:nil error:&err];
or change the type of "encoding" hope it will solve you problem.

Try This
NSString *pathtest = [[NSBundle mainBundle] pathForResource:#"dirty" ofType:#"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:pathtest encoding:NSASCIIStringEncoding error:&error];
NSLog(#"Test: %#",content);

Related

Load NSAttributedString from File

Is there a way to have some sort of rich text in a file, and load the file into an NSAttributedString? I looked into loading a .rtf file into an NSAttributed String:
NSString *filePathFooter = [[NSBundle mainBundle] pathForResource:kFooterFileName ofType:#"rtf"];
NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:[NSURL URLWithString:filePathFooter]
options:nil
documentAttributes:NULL
error:&error];
But this leaves me with:
Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)"
Is there a way to do this?
Use URLForResource instead, and pass the resulting URL like so:
NSURL *url = [[NSBundle mainBundle] URLForResource:kFooterFileName withExtension:#"rtf"];
NSError *error = nil;
NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:url
options:nil
documentAttributes:NULL
error:&error];
NSCocoaErrorDomain error code 258 is NSFileReadInvalidFileNameError, so it looks like it can't find the file you're passing.

Cocoa error 256, When using initWithContentsOfURL:

i getting data from my site:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
contents have Cocoa error 256. where i wrong?
The issue is in Hebrew characters, you should html-escape them, also try request with English characters instead, to see if it works
- (void)yourMethod
{
NSString *name = #"שימרגוליס";
name = AFURLEncodedStringFromStringWithEncoding(name, NSUTF8StringEncoding);
NSString *website = [NSString stringWithFormat:#"http://www.ba-cafe.com/fbconnect.php?email=%#&name=%#&pass=SwHyK17!",#"email#mail.com",name];
NSLog(#"%#", website);
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:website] encoding:NSUTF8StringEncoding error:&error];
}
Where AFURLEncodedStringFromStringWithEncodingis a function from AFNetworking framework
Check the Console log for NSLog(#"%#", website);
You will see something like this:
http://www.mysite.com/fbconnect.php?email=thetrimmedname&name=emailaddress&pass=***
So do this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", [jsonObject objectForKey:#"email"], nameTrimmmed ];
instead of this:
NSString *website = [NSString stringWithFormat:#"http://www.mysite.com/fbconnect.php?email=%#&name=%#&pass=***", nameTrimmmed, [jsonObject objectForKey:#"email"]];
This is because of the dot in the email address.
Look right here:
Error while trying to access Google translate with NSURL

initWithContentsOfFile Deprecated

I was wondering what code I could use instead of initWithContentsOfFile: as I've been searching for something that isn't deprecated but cannot find anything. I'm trying to display a local HTML file within a webview, below is the code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath];
[self.myWebView loadHTMLString:html baseURL:bundleUrl];
}
And I'm getting a warning saying that initWithContentsOfFile: is deprecated and would like to know how to update it.
Try this:
NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:#"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error];
Use the newer initializer:
NSString's [initWithContentsOfFile: encoding: error:] method, where you can get a useful error passed back to you if there are any problems. I've linked the documentation for you. If you don't know the encoding of the file, you can also use another method that has a usedEncoding parameter.

How to use method stringWithContentsOfFile:encoding:error

I don't know how to use this method.
I want to get the data in an rtf file as a NSString, however I can not.
Here is part of my code.
NSString *filePath = #"path of the file";
NSString *ImagePath = [[[NSString alloc] init] autorelease];
imagePath = [NSString stringWithContentsOfFile: filePath encoding:(NSStringEncoding)nil error:(NSError**) nil];
I can't readout the data in that .rtf file. I am appreciate that everyone may help me. Thanks.
Try using this
imagePath = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:nil];
Here is the function
NSString *content = [[[NSString alloc] initWithContentsOfFile:filepath
usedEncoding:nil
error:nil] autorelease];
before using this be sure u giving a right path (filepath) to the string.

Unable to retrieve data in string

self.pdfPath = [[NSBundle mainBundle] pathForResource:#"iPhone_SDK_License" ofType:#"pdf"];
NSData *htmlData = [NSData dataWithContentsOfFile:pdfPath];
NSString *htmlstr = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
It is showing nil in htmlstr, so need some assistance on it.
A pdf is not an html file so I assume that initWithData:encoding: just cannot understand the data and so fails.
Use stringWithContentsOfURL:encoding:error: to see the details of the error.