<small id='GxebC'></small><noframes id='GxebC'>

    <i id='GxebC'><tr id='GxebC'><dt id='GxebC'><q id='GxebC'><span id='GxebC'><b id='GxebC'><form id='GxebC'><ins id='GxebC'></ins><ul id='GxebC'></ul><sub id='GxebC'></sub></form><legend id='GxebC'></legend><bdo id='GxebC'><pre id='GxebC'><center id='GxebC'></center></pre></bdo></b><th id='GxebC'></th></span></q></dt></tr></i><div id='GxebC'><tfoot id='GxebC'></tfoot><dl id='GxebC'><fieldset id='GxebC'></fieldset></dl></div>
      <bdo id='GxebC'></bdo><ul id='GxebC'></ul>
  1. <legend id='GxebC'><style id='GxebC'><dir id='GxebC'><q id='GxebC'></q></dir></style></legend>
    <tfoot id='GxebC'></tfoot>

    1. UIWebView 响应 Javascript 调用

      时间:2023-10-22
    2. <legend id='Qli2B'><style id='Qli2B'><dir id='Qli2B'><q id='Qli2B'></q></dir></style></legend>
    3. <tfoot id='Qli2B'></tfoot>

      <small id='Qli2B'></small><noframes id='Qli2B'>

      <i id='Qli2B'><tr id='Qli2B'><dt id='Qli2B'><q id='Qli2B'><span id='Qli2B'><b id='Qli2B'><form id='Qli2B'><ins id='Qli2B'></ins><ul id='Qli2B'></ul><sub id='Qli2B'></sub></form><legend id='Qli2B'></legend><bdo id='Qli2B'><pre id='Qli2B'><center id='Qli2B'></center></pre></bdo></b><th id='Qli2B'></th></span></q></dt></tr></i><div id='Qli2B'><tfoot id='Qli2B'></tfoot><dl id='Qli2B'><fieldset id='Qli2B'></fieldset></dl></div>
            • <bdo id='Qli2B'></bdo><ul id='Qli2B'></ul>
                <tbody id='Qli2B'></tbody>
                本文介绍了UIWebView 响应 Javascript 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何像 Mobile Safari 那样拦截诸如 window.open 之类的 Javascript 调用?我还没有看到任何关于此的列表,但它一定有可能吗?

                How can I intercept Javascript calls such as window.open as Mobile Safari does? I haven't seen anything listed about this, but it must be possible somehow?

                以前有人做过吗?

                推荐答案

                当页面完成加载(webViewDidFinishLoad:) 时,注入一个 window.open 覆盖.当然,它不适用于在页面加载期间调用的 window.open.然后使用自定义方案来回调您的目标 C 代码.
                好的,我已经测试过了.现在可以了.
                创建一个新的基于视图"的项目,并使用 IB 在 viewcontroller xib 中添加一个 webview.

                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.
                OK I have tested it. Now it works.
                Create a new "view based" project and add a webview in the viewcontroller xib using IB.

                @implementation todel2ViewController
                
                
                // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
                - (void)viewDidLoad {
                    [super viewDidLoad];
                    NSString* page = @"<html><head></head><body><div onclick='javascript:window.open("http://www.google.com");'>this is a test<br/>dfsfsdfsdfsdfdsfs</div></body></html>";
                    [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
                

                Javascript:

                The Javascript:

                var open_ = window.open; 
                window.open = function(url, name, properties)  
                {   
                    var prefix = 'csmobile://';
                    var address = url; 
                    open_(prefix + address); 
                    return open_(url, name, properties); 
                }; 
                

                日志

                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/
                

                这篇关于UIWebView 响应 Javascript 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 UIWebView 中搜索? 下一篇:Webview 中的 PDF 性能

                相关文章

                <tfoot id='XrlPj'></tfoot>

                • <bdo id='XrlPj'></bdo><ul id='XrlPj'></ul>

                <small id='XrlPj'></small><noframes id='XrlPj'>

                  <i id='XrlPj'><tr id='XrlPj'><dt id='XrlPj'><q id='XrlPj'><span id='XrlPj'><b id='XrlPj'><form id='XrlPj'><ins id='XrlPj'></ins><ul id='XrlPj'></ul><sub id='XrlPj'></sub></form><legend id='XrlPj'></legend><bdo id='XrlPj'><pre id='XrlPj'><center id='XrlPj'></center></pre></bdo></b><th id='XrlPj'></th></span></q></dt></tr></i><div id='XrlPj'><tfoot id='XrlPj'></tfoot><dl id='XrlPj'><fieldset id='XrlPj'></fieldset></dl></div>

                    <legend id='XrlPj'><style id='XrlPj'><dir id='XrlPj'><q id='XrlPj'></q></dir></style></legend>