Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When the page is finished loaded (webViewDidFinishLoad:), inject a window.open override. Of course it will not work for window.open which are called during page load. Then use a custom scheme to callback your objective C code.<br> [EDIT] OK I have tested it. Now it works.<br> Create a new "view based" project and add a webview in the viewcontroller xib using IB.</p> <pre><code>@implementation todel2ViewController // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; NSString* page = @"&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;div onclick='javascript:window.open(\"http://www.google.com\");'&gt;this is a test&lt;br/&gt;dfsfsdfsdfsdfdsfs&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;"; [self.view loadHTMLString:page baseURL:[NSURL URLWithString:@""]]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString* urlString = [[request URL ] absoluteString ]; NSLog(@"shouldStartLoadWithRequest navigationType=%d", navigationType); NSLog(@"%@", urlString); if ([[[request URL] scheme] isEqualToString:@"myappscheme"] == YES) { //do something NSLog(@"it works"); } return YES; } - (void)webViewDidFinishLoad:(UIWebView *)webView { //Override Window NSString*override = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NewWindow" ofType:@"js"] encoding:4 error:nil]; [self.view stringByEvaluatingJavaScriptFromString:override]; } @end </code></pre> <p>The Javascript:</p> <pre><code>var open_ = window.open; window.open = function(url, name, properties) { var prefix = 'csmobile://'; var address = url; open_(prefix + address); return open_(url, name, properties); }; </code></pre> <p>The log </p> <pre><code>2011-07-05 14:17:04.383 todel2[31038:207] shouldStartLoadWithRequest navigationType=5 2011-07-05 14:17:04.383 todel2[31038:207] myappscheme:it%20works 2011-07-05 14:17:04.384 todel2[31038:207] it works 2011-07-05 14:17:04.386 todel2[31038:207] shouldStartLoadWithRequest navigationType=5 2011-07-05 14:17:04.386 todel2[31038:207] http://www.google.com/ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload