InitWithContentsOfFile Deprecated

I was wondering what code I could use instead of initWithContentsOfFile: since I was looking for something that is not deprecated but cannot find anything. I am trying to display a local HTML file in 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 get a warning that initWithContentsOfFile: is deprecated and would like to know how to update it.

+8
iphone cocoa-touch webview
source share
2 answers

Try the following:

 NSError *error = nil; NSString *string = [[NSString alloc] initWithContentsOfFile:@"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error]; 
+16
source share

Use the new initializer:

NSString [initWithContentsOfFile: encoding: error:] , where you can get a useful error passed to you if you encounter any problems. I have linked the documentation for you. If you do not know the encoding of the file, you can also use another method with the usedEncoding parameter.

0
source share

All Articles