Unable to retrieve data in string - iphone

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.

Related

Read string from a file return nil

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

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.

grabbing data from an internal file

I'm grabbing data from an external file using NSURL with the code below. If i wanted to grab this data from an internal file in my resources folder how would the code be different.
here's the NSURL code:
NSURL *dataUrl = [NSURL URLWithString:#"https://sites.google.com/site/*****/***/file.asc"];
NSString *fileString = [NSString stringWithContentsOfURL:dataUrl encoding:NSUTF8StringEncoding error:nil];
this was my attempt:
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"asc"];
NSString *fileString = [NSString initWithContentsOfFile:path];
thanks in advance
Maybe try stringByExpandingTildeInPath
something like
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"asc"];
path = [path stringByExpandingTildeInPath];
NSString *fileString = [NSString initWithContentsOfFile:path];

how to Download and save xml in documents and then how to parse xml file from documents?

In my app app i have to parsing a xml file downloaded from internet. How to download this xml and save in documents on iphone ? and then how can i start the parsing of XML saved in documents??
You mean something like that
NSString *URLString = #"http://www.example.com/XML/note.xml";
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
NSData *dataXML = [NSData dataWithContentsOfURL:URL];
NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"sample.xml"];
[dataXML writeToFile:storePath atomically:TRUE];
NSData *contenuto = [NSData dataWithContentsOfFile:storePath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:contenuto]
Download the contents to an NSData object when it's loaded for the first time. Save it to disk using either one of
– writeToFile:atomically:
– writeToFile:options:error:
Which are described in the NSData class reference here: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/writeToFile:atomically:
When you want to load it from disk, use [NSData dataWithContentsOfFile:] to load it into an NSData object again.
As for parsing NSData into XML, look into other answers to this very, very common question, e.g. How do I parse an NSString containing XML in Objective-C?