Tutorial :iPhone SDK: UIWebView


I'm working on an app that uses a UIWebView to display its help files. The webView lives in it's own view, DocViewController...when its called the

- (void)viewDidLoad {  

method uses

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:   [[NSBundle mainBundle] pathForResource:docPage ofType:@"html"]isDirectory:NO]]];  

to load the proper doc page. However, once the first page is loaded, the view becomes static and new pages aren't loaded when the docPage changes and the view is toggled. Is there a way to clear the webView so new pages load when requested?

The first answer is confusing to me. As is the routine below works. It's just that it only works once. After the view is loaded the first time it does not change when this view is toggled on again and the requested html page is different. The view always displays the first html page requested and will not load new requests.

 - (void)viewDidLoad {         docPage = [NSString stringWithFormat: @"%d", hexpage];         [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[NSBundle mainBundle] pathForResource:docPage ofType:@"html"]isDirectory:NO]]];     }  


viewDidLoad is only called once, unless the view is released and needs to be reloaded. This occurs usually only when the view controller receives a low memory warning. Instead of viewDidLoad, try putting that code in viewWillAppear:, which gets called every time the view will show on the screen.


Try using

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL  

passing an NSURL object to the dir that contains your HTML as baseURL and loading the HTML string with something like

NSString *path = [[NSBundle mainBundle] pathForResource:@"myfile"                                                    ofType:@"html"];  NSString *html = [NSString stringWithContentsOfFile:path                                              encoding:NSUTF8StringEncoding                                                error:nil];  

Edit: Also, make sure that your hyperlinks are not trying to open in a new window with something like target="_blank", your webview will not open those

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »