Perhaps opening popup links in UIWebView?

I have a UIWebView that I use as a built-in browser in my application.

I noticed that links on web pages that open new windows are ignored without calling my code.

I tried a breakpoint on

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

and then select the link that will open the popup and the breakpoint will never be deleted. Is there something I can do to intercept this pop-up link selection and get the url and just load it normally?

I am not interested in displaying the popup in the application itself, I just want the URL of what will load in the popup to load into the main webview.

Is it possible?

Thanks!

+6
objective-c iphone uikit popup uiwebview
source share
4 answers

I came across this, and rewriting HTML was the best solution I could come up with. The biggest problem I came across with this approach is that the web browser is interactive for a couple of seconds until the webViewDidFinishLoad method is called: so the links seem to be broken for a few seconds until they are overwritten.

There are three areas that I rewrote: links, form posts, and window.open () calls.

I used a similar approach for the first code cut off in Jasarian's answer to rewrite the purpose for links and forms by repeating tags and forms. To override window.open, I used code similar to the following:

 var oldWindowOpen = window.open; window.open = function(url, sName, sFeatures, bReplace) { oldWindowOpen(url, '_self'); }; 
+7
source share

So, after a little research, it turned out that the UIWebView class intentionally ignores the links that will open in a new window (either using the "target" element in the tag, or using javascript in the onClick event).

The only solutions I found was to manipulate the html pages using javascript. Although this works in some cases, it is not bulletproof. Here are some examples:

 links = document.getElementsByTagName('a'); for (i=0; i<links.length; i++) { links[i].target='_self'; } 

This will change all links that use the target element to point to _self, instead of _blank or _new. This will probably work in all directions and does not present any problems.

Another snippet I found followed the same idea, but with the onClick event:

 links = document.getElementsByTagName('a'); for (i=0; i<links.length; i++) { links[i].onclick=''; } 

This is just nasty. It will only work if the link tag has the correct href element, and only if the onclick event is used to open a new window (using window.open () or something similar). The reasons why this is unpleasant should not be explained, but one example can be if onClick is used for something other than opening a window - this is a very common case.

I assume that you can continue with this and start executing some line corresponding to the onClick method and check for window.open (), but again, this is really far from ideal.

+3
source share

Here's how I get Twitter links (for example, a link to pages that try to open with new windows):

 -(BOOL)webView:(UIWebView *)mainWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (navigationType == UIWebViewNavigationTypeLinkClicked) { //Allows for twitter links [self.mainWebView loadRequest:request]; return NO; } return YES; } 
+2
source share
  WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; theConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = YES; webView1 = [[WKWebView alloc] initWithFrame:self.webView.frame configuration:theConfiguration]; webView1.navigationDelegate = self; webView1.UIDelegate = self; [self.view addSubview:webView1]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { NSString *htmlString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"htmlString: %@", htmlString); [webView1 loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"your url"]; }]; [dataTask resume]; 
0
source share

All Articles