initWithContentsOfFile Deprecated - iphone

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.

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

Webview displaying one HTML file in iPhone sdk

I have three HTML files in my local resources bundle. I need to display it as a reader. So i used a web view to display that using following code,
- (void)viewDidLoad
{
[super viewDidLoad];
totalArray=[[NSMutableArray alloc]init];
[totalArray addObject:#"file1"];
[totalArray addObject:#"file2"];
[totalArray addObject:#"file3"];
NSLog(#"totalArray count:%d",[totalArray count]);
for (int i=0;i<3;i++)
{
NSLog(#"i count:%d",i);
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
NSLog(#"webview %#", bundlePath);
NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:i] ofType:#"html"];
NSLog(#"filePath1:%#",filePath1);
[htmlView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath1]]];
}
}
Am getting the total count as 3, current index counts and file path. But still it displays only my first html file. My other two files were missing. What could be the problem here? Kindly help me. Thanking you.
You need wait till the file are loaded , looping is not god idea , instead you can achieve same using below code. Use fileURLWithPath to load local file.
- (void)viewDidLoad
{
[super viewDidLoad];
count = 0;
totalArray=[[NSMutableArray alloc]init];
[totalArray addObject:#"file1"];
[totalArray addObject:#"file2"];
[totalArray addObject:#"file3"];
[_htmlView setDelegate:self];
NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:count] ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:filePath1];
[_htmlView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
count++;
if (count < 3) {
NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:count]
ofType:#"html"];
NSLog(#"filePath1:%#",filePath1);
NSURL *url = [NSURL fileURLWithPath:filePath1];
[_htmlView loadRequest:[NSURLRequest requestWithURL:url]];
}
}
For the local file urls you have to use fileURLWithPath: method of NSURL. Also, you could get file url from the bundle with [[NSBundle mainBundle] URLForResource: withExtension:] method. Further more, I don't think that loading urls in the loop is a good idea, you should try something else like loading url on button action. Good Luck!

what is the wrong when loading local resource files in webview in iphone

I am trying to load local css and html files into WebView , but the web view displays the content of css file in webview as you see in the following image ..
objective-c code:
NSString *htmlPath= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlPath];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
what is the problem ?
You must not use
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
instead loadHTMLString try to use stringByEvaluatingJavaScriptFromString
And if you have any problem check the following post. I think you will get it easily
UIWebView and local css file
I have not used it for CSS but hope this will help you...Good Luck...

memory leaks this code

htmPath = [[NSBundle mainBundle] pathForResource:#"barcode" ofType:#"html"];
params = [[NSString alloc] initWithFormat:#"?data=%#",numberTextField.text];
fileURL = [NSURL URLWithString:[[[NSURL fileURLWithPath:htmPath] absoluteString] stringByAppendingString:params]];
[self.barView loadRequest:[NSURLRequest requestWithURL:fileURL]];
////htmpath,params are strings.
///fileurl is nsurl and in this i m calling appending two strings for the result.
///numberTextField.text is the textfield that will use this html for further functionality.
you have allocated the "params" and did not released it.
params = [[NSString alloc] initWithFormat:#"?data=%#",numberTextField.text];
you should release every allocated object.
[params release];

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.