UIWebView UI Loading

I am creating an application that will display some newsletters for users. newsletters are displayed in uiWebView. I am reading the newsletter URL from the RSS feed. I am parsing xml and in the form of a table I have all the newsletters. By clicking on the cell, the uiWebView sets up and loads the newsletter. Since uiWebView does not open links that have target = _blank, I need to replace _blank with the target to "". In NSOperation, I load the contents of html and after the download is complete, I replace the lines as follows:

NSMutableString *page = [[[NSMutableString alloc] initWithData:pageData encoding:NSISOLatin1StringEncoding] autorelease]; [page replaceOccurrencesOfString:@"target=_blank" withString:@"target=""" options:0 range:NSMakeRange(0, [page length])]; 

after that, I load the parsed string into the webView to display it to the user.

 [myWebView loadHTMLString:page baseURL:[NSURL URLWithString:@""]]; 

while NSOperation loads the contents of the HUD page with an activity indicator and a label that displays โ€œDownloadโ€. When the download is complete, the HUD is removed from the supervisor.

so far so good ... but here are the questions. I used NSOperation with a callback function because I was unable to determine the last call to webDidFinishLoading (I read this UIWebView - how to identify the message "last" webViewDidFinishLoad "? is part of my problem, but the answer was to use some private Apple classes, and this problem for me). so i do this:

 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *url = request.URL; NSString *urlString = url.absoluteString; NSLog(@"WebViewSouldStartWithRequest: %@", urlString); if (navigationType == UIWebViewNavigationTypeLinkClicked) { if(![urlString isEqualToString:@"about:blank"]) { NSLog(@"FeedViewController: startPageLoad"); activityHud = [[HUDView alloc] initWithFrame:CGRectMake(110, 100, 80, 80)]; [activityHud setText:@"Loading"]; [myWebView addSubview:activityHud]; NSLog(@"FeedViewController: pageUrl = %@", urlString); [self resetWebView]; [urlLoader loadPageFromUrl:request.URL withCallbackTarget:self withCallbackSelector:@selector(endLoading:)]; } } return NO; 

}

 - (void) endLoading:(NSMutableString *)page { [self resetWebView]; NSLog(@"FeedViewController: endLoading"); [activityHud removeFromSuperview]; [myWebView loadHTMLString:page baseURL:[NSURL URLWithString:@""]]; } 

after I touch the cell in the table view and the newsletter is shown, it looks the way it should, when I click the link in the newsletter, the page loads with a new request, it is analyzed, but it doesnโ€™t visually look the way it should (I looked at the page after parsing in NSLog, and it had css styles and html tags, head, body open and closed correctly), who had this problem with uiWebView without displaying web pages correctly?

I tried loading the page with - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ return YES; } - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ return YES; } and in - (void) webViewDidFinishLoad: (UIWebView *) webview {} and in - webViewDiStartLoad

but the methods are called for every element loaded in the web view, so showing and hiding the HUD in this method is not a good solution.

I ran into some problems when using - (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType {... return NO}

for example, links that have paths like this / bilder -galerien to not work, and in NSLog I get this for them

Unknown scheme doing nothing: / Bilder -Galerien

but when i use

  • (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType {return Yes} and didstartLoading and finishLoading URLs loaded, and I don't know why ...

Another problem is special German characters. In my first load (after clicking a cell in uiTableView) the page is analyzed, as it should be using uiWebView, but after I click on the link and the corresponding request loads, the characters are not processed correctly.

Can someone point me in a good direction? thanks in advance

+6
string html iphone parsing uiwebview
source share
2 answers

UIWebView does not open target = "_ blank" links. Some links do not fire UIWebViewNavigationTypeLinkClicked events. This happens when the link has the target = "_ blank" attribute.

To get around this problem, I used the code below. It introduces some javascript after loading the page to remove the target attribute from all links. After using this code, I no longer needed to parse the html source and, for example, replaced _blank with _self.

 - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *js = @"\ var d = document.getElementsByTagName('a');\ for (var i = 0; i < d.length; i++) {\ if (d[i].getAttribute('target') == '_blank') {\ d[i].removeAttribute('target');\ }\ }\ "; [webView stringByEvaluatingJavaScriptFromString:js]; } 
+4
source share

I like the answer of SorinA, we can do it even better:

  • avoid javascript blocking with var d
  • since we know that we are running the webkit engine, we can use the selector:

... therefore, in your UIWebViewDelegate protocol method, js code is called:

 - (void) webViewDidFinishLoad: (UIWebView *) webView { [webView stringByEvaluatingJavaScriptFromString: @"(function($){for(var i=0;i<$.length;++i){$[i].removeAttribute('target')}})(document.querySelectorAll('a[target=_blank]'))"]; } 
+3
source share

All Articles