单击链接时 UIWebView 不调用 shouldStartLoadWithRequest ||在 UIWebView 中捕获链接点击 [英] UIWebView not calling shouldStartLoadWithRequest when link is clicked || Capturing link clicks in UIWebView

查看:27
本文介绍了单击链接时 UIWebView 不调用 shouldStartLoadWithRequest ||在 UIWebView 中捕获链接点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个问题需要比可可知识更多的网络知识.我只知道网络的基础知识被难住了.这也有点边缘情况(虽然我的样本量很小).

So this question requires more web knowledge than cocoa knowledge. I know only the basics of web am stumped. This is also a bit of an edge case (although my sample size is small).

我正在尝试将视频从 youtube 交叉发布到我们的应用中.

I'm playing around with a feature to cross-post videos from youtube into our app.

所以我有一个 browserController 并且我加载了 http://m.youtube.com 并且它加载得很好.shouldStartLoadWithRequest 在初始加载时被调用.但是,如果我单击链接转到视频页面,则永远不会调用 shouldStartLoadWithRequest.

So I have a browserController and I load http://m.youtube.com and it loads fine. shouldStartLoadWithRequest is called upon initial load. However, if I click a link to go to a video's page shouldStartLoadWithRequest is never called.

我尝试了几个其他网站(nba.com 和 vimeo.com),它们按预期工作,当点击链接时会调用 shouldStartLoadWithRequest,这让我相信有我的 browserController 和我的 UIWebView 代码都没有问题,这是 youtube 网站处理链接点击的方式所特有的.

I've tried out a couple other sites (nba.com and vimeo.com) and they work as expected, shouldStartLoadWithRequest IS called when a link is clicked, so that leads me to believe there's nothing wrong with my browserController nor my UIWebView code and it's something particular to the way the youtube site is handling it's link clicks.

知道发生了什么以及如何确保 shouldStartLoadWithRequest 被调用,或者以其他方式收听链接点击吗?

Any idea what's going on and how to either make sure shouldStartLoadWithRequest is called or else listen to the link click otherwise?

附注.我不知道如何标记它,以便它指代这可能涉及哪些 Web 框架.

PS. I had no idea how to tag this so that it refers to which web frameworks this might involve.

推荐答案

我决定走一条不同的道路,并创建了一个 NSTimer,它使用 javascript 定期抓取当前 url.当它发生变化时,我知道页面切换了.如果调用 shouldStartLoadWithRequest,我会暂时禁用计时器.我在成功加载时重新启用它.

I decided to go a different route and created an NSTimer that periodically grabs the current url using javascript. When it changes, I know the page switched. I disable the timer briefly if shouldStartLoadWithRequest is called. I reenable it on successful load.

它并不完美,但足以满足我的需求并且适用于 m.youtube.com.我想有一些更高级的 javascript 也可以专门获取链接点击次数,但只需检查 url 更新就可以了.

It's not perfect, but works well enough for my needs and works on m.youtube.com. I imagine there is some more advanced javascript that could specifically get the link clicks also, but just checking for url updates works ok.

// assume self.currentURL and self.updateTimer exist in controller

// check every 2 seconds
-(void) startTimer {
    [self stopTimer];
    self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                        target:self
                                                      selector:@selector(timerUpdate)
                                                      userInfo:nil
                                                       repeats:YES];
}

-(void) stopTimer {
    [self.updateTimer invalidate];
    self.updateTimer = nil;
}

-(void) timerUpdate {

    NSString *title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSString *url = [self.webView stringByEvaluatingJavaScriptFromString:@"document.URL"];

    if (![url isEqualToString:self.currentURL]) {
        NSLog(@"New URL: %@", url);
        self.currentURL = url;
    }

    // update back/forward buttons here 
}

更新:不幸的是,目前针对这个特定问题的所有解决方案都有些小技巧.这个演讲给出了一些其他的想法:http://vimeo.com/61488911

UPDATE: Unfortunately all the solutions for this particular problem right now are a little bit of a hack. This talk gives some other ideas: http://vimeo.com/61488911

选项归结为:

  1. 用于捕获页面更改的 JavaScript 注入(阅读 history.pushState).
  2. KVO 用于监视 webview 的更改(如滚动视图大小调整)
  3. 观察标题的变化,如上.
  4. 每次用户点击视图时检查更新.

使用适合您需求的任何一种.

Use whichever one makes sense for your needs.

这篇关于单击链接时 UIWebView 不调用 shouldStartLoadWithRequest ||在 UIWebView 中捕获链接点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆