有没有人发现某些URL的UIWebView失败了? [英] Has anyone found that UIWebView fails for some URLS?

查看:135
本文介绍了有没有人发现某些URL的UIWebView失败了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在iPhone应用程序上工作了几个星期,我添加的早期功能之一是 UIWebView ,它加载了上下文敏感的维基百科页面主题。这实现起来非常简单,并且已经工作了一段时间。



今天,我发现功能意外停止了。我一直在那条代码的周边摆弄,最初认为我已经破坏了一些东西。



我检查了所有显而易见的地方,我的网址是 UIWebView 仍然连接在XIB等。我没有发现任何问题。



进一步调查,我在 UIWebViewDelegate didFailLoadWithError 上遇到了一些错误处理,发现我收到了-999错误:


NSURLErrorCancelled



异步时返回加载是
取消。



一个Web Kit框架委托将在
对加载$执行
取消操作时收到此错误b $ b资源。请注意,如果下载
取消,NSURLConnection
或NSURLDownload委托将不会收到此错误。


所以这听起来像是在原始请求完成之前发出新请求(或取消)。我检查了我的代码是否有这样的东西,然后空了。



所以我进入了我惯常的偏执狂,并认为维基百科阻止了基于UAgent或其他东西的请求并继续进行一些疯狂的追逐,试图欺骗我回到一个幸福的地方。这些尝试没有成功,最终理智得到了胜利。我创建了一个简单的python脚本来模仿我在模拟器中从我的APP发出的HTTP请求,看看维基百科发回的内容:

  string =GET /wiki/Franklin_D._Roosevelt HTTP / 1.1 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ .wikipedia.org / wiki / Franklin_D._Roosevelt \\\\ n接受:* / * \\\\ nnAccept-语言:en-us \\\\ nn-接受 - 编码:gzip,deflate \\ nnConnection :keep-alive\r\\\
\r\ n
import socket
s = socket.socket()
s.connect((en.wikipedia.org, 80))
s.send(字符串)
x = s.recv(1000)
而(x):
print x
x = s.recv(1000)

所以我经营这个家伙并发现维基百科非常友好地及时完整地返回我的数据。那么是怎么回事?



Chinks开始出现在我现在的每一个偏执的盔甲中这总是我的错,我决定查看其他iPhone应用程序是否可以查看这些网址。我发布了一个推文,带有讽刺性的趣味(我很容易被逗乐),并查看Tweetie可以查看URL。它不能。



朋友在Twitterific中尝试一下。同样的问题。在Safari和维基百科应用程序中工作正常,但似乎使用沼泽标准 UIWebView 的iPhone应用程序在维基百科页面上遇到问题。



Just为了完全确定没有其他变量,我创建了一个简单测试应用一个UIWebView加载 http ://en.wikipedia.com ,它失败并出现相同的错误(最后添加了代码)。



那么做什么呢你觉得呢?这只是一个 UIWebView 错误吗?任何苹果公司的人都知道这里发生了什么?



我是否错过了一些完全明显的东西,我再一次坐火车上班,没穿裤子?



期待听到您的想法。请注意,昨天工作正常。



汇报(或者说Post Mortem更合适):<​​/ p>

感谢 Duncan 提供的解决方案以及对此处发生的事情的清晰描述。



看起来我最初看到错误的原因,当我没有实现 didFailLoadWithError 委托方法时,是默认行为对于该方法显然是要清除 UIWebView 并因此终止请求。当我添加我的实现以了解发生了什么时,我坚持使用一些代码将错误写入视图,正如Duncan所指出的那样,这就是我得到的结果。



似乎是一个非常糟糕的解决方案,可以忽略回调中的-999错误代码,但我对胶带很好。



我尝试了不少应用来测试如果这是一个UIWebView问题(Tweetie,Twitteriffic等...),他们都有问题。看起来这对开发人员来说可能是一个非常普遍的疏忽。也许Apple可以在下一个版本中清理它。



其他有趣的一点是,当我切换我的URL以使用 http://en.m.wikipedia.com 而不是 http://en.wikipedia.com ,问题消失了。

  //实施viewDidLoad加载视图后的其他设置,通常来自笔尖。 
- (void)viewDidLoad {
[super viewDidLoad];
_webView =(UIWebView *)self.view;
_webView.delegate = self;

//加载网址

// FAIL
// NSURL * newUrl = [[[NSURL alloc] initWithString:@http:// en。 wikipedia.com] autorelease];

// OK
NSURL * newUrl = [[[NSURL alloc] initWithString:@http://www.stackoverflow.com] autorelease];

NSURLRequest * newUrlRequest = [NSURLRequest requestWithURL:newUrl];
[_webView loadRequest:newUrlRequest];
}

//委托东西
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType {return YES; }
- (void)webViewDidStartLoad:(UIWebView *)wv
{
//开始加载,在状态栏中显示活动指示符
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible =是;
}

- (无效)webViewDidFinishLoad:(UIWebView *)webView
{
//已完成加载,隐藏状态栏中的活动指示符
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSURLErrorDomain
// load error,隐藏状态栏中的活动指示符
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO;
[_webView loadHTMLString:[[[NSString alloc] initWithFormat:@无法加载页面%@,[error localizedDescription]] autorelease] baseURL:nil];
}


解决方案

看起来这里有一个答案:



如何在iPhone 3.0操作系统中修复NSURLErrorDomain错误-999



反过来指的是:



http://www.iphonedevsdk.com/forum/iphone-sdk-development/6280-uiwebview-didfailloadwitherror-how-get-errors-code-list.html



在webView中看起来你需要这样的黑客:didFailLoadWithError:delegate:

  if([错误代码]!= NSURLErrorCancelled){
//显示错误警告等等
}

本质上发生的事情是委托正在获得取消(-999)失败,可能是或在javascript或者甚至在UIWebView错误中iginated。



使用你的代码,webview根本不显示任何内容,因为你使用的是相同的UIWebView来显示错误。如果您只是NSLog的错误,您会看到失败,但然后页面加载得很好,给你一个暗示失败是假的。


I have been working on an iPhone App for a couple of weeks now and one of the earlier features I added was a UIWebView that loaded context sensitive Wikipedia page topics. This was pretty trivial to implement and has been working fine for some time.

Today, I found that functionality had unexpectedly stopped working. I had been fiddling around at the perimeter of that piece of code and initially assumed that I had broken something.

I checked all the obvious places, my urls, was the UIWebView still hooked up in the XIB etc. I didn't find any issues.

Investigating further, I stuck some error handling on my UIWebViewDelegate didFailLoadWithError and found I was getting a -999 error:

NSURLErrorCancelled

Returned when an asynchronous load is canceled.

A Web Kit framework delegate will receive this error when it performs a cancel operation on a loading resource. Note that an NSURLConnection or NSURLDownload delegate will not receive this error if the download is canceled.

So this sounds like making a new request (or cancelling) before the original one has finished. I check my code for anything like this and come up empty.

So I went into my usual downward spiral of paranoia and assumed that Wikipedia was blocking requests based on UAgent or something and went on something of wild goose chase attempting to spoof my way back to a happy place. These attempts were not successful and eventually sanity prevailed. I created a simple python script to mimic the HTTP request I was making from my APP in the simulator, to see what Wikipedia was sending back:

string = "GET /wiki/Franklin_D._Roosevelt HTTP/1.1\r\nHost: en.wikipedia.org\r\nUser-Agent: test\r\nReferer: http://en.wikipedia.org/wiki/Franklin_D._Roosevelt\r\nAccept: */*\r\nAccept-Language: en-us\r\n_Accept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n\r\n"
import socket
s = socket.socket()
s.connect(("en.wikipedia.org",80))
s.send(string)
x = s.recv (1000)
while (x):
    print x
    x = s.recv (1000)

So I run this guy and discover that Wikipedia is very kindly returning my data promptly and completely. So what is going on?

Chinks started to appear in my every present, paranoid armour "It's always my fault" and I decide to check out if other iPhone apps can view these URLs. I post a tweet with an ironically amusing (I am easily amused) URL and check out if Tweetie can view the URL. It can't.

A friend tries it out in Twitterific. Same problem. Works fine in Safari and the Wikipedia App, but it seems like iPhone apps using a bog standard UIWebView are having problems with Wikipedia pages.

Just to be completely certain there are no other variables, I created a simple test app with just a UIWebView that loads up http://en.wikipedia.com, it fails with the same error (added that code at the end).

So what do you guys think? Is this just a UIWebView bug? Any Apple gronks out there know what is going on here?

Have I missed something completely obvious and I am once again sitting on the train to work without my pants on?

Looking forward to hearing what you think. Keep in mind, this was working fine yesterday.

Debrief (or maybe Post Mortem is more appropriate):

Thanks Duncan for the solution and a pretty clear description of what is going on here.

Looks like the reason I originally saw the error, when I wasn't implementing the didFailLoadWithError delegate method at all, was that the default behavior for that method is apparently to clear the UIWebView and consequently kill the request. When I added my implementation to find out what was going on, I stuck in some code to write the error to the view and, as Duncan points out, this is what got me.

Seems like a pretty horrible solution to ignore -999 error codes in the callback, but I am fine with duct tape.

I tried quite a few apps to test if this was a UIWebView problem (Tweetie, Twitteriffic etc ...) and they all had the issue. It looks like this might be a pretty common oversight for developers. Maybe Apple can clean this up in the next version.

Other interesting point is that when I switched my URLs to use http://en.m.wikipedia.com instead of http://en.wikipedia.com, the problem went away.

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    _webView = (UIWebView*)self.view;
    _webView.delegate = self;

    // load the url

    // FAIL
    //NSURL * newUrl = [[[NSURL alloc] initWithString:@"http://en.wikipedia.com"] autorelease];

    // OK
    NSURL * newUrl = [[[NSURL alloc] initWithString:@"http://www.stackoverflow.com"] autorelease];

    NSURLRequest * newUrlRequest = [NSURLRequest requestWithURL:newUrl];
    [_webView loadRequest:newUrlRequest];                                    
}

// delegate stuff
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType { return YES; }
- (void)webViewDidStartLoad:(UIWebView *)wv
    {
    // starting the load, show the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // finished loading, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSURLErrorDomain
    // load error, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [_webView loadHTMLString:[[[NSString alloc] initWithFormat:@"Failed to load page %@", [error localizedDescription]] autorelease] baseURL:nil];
}

解决方案

Looks like there's an answer here:

How do I fix NSURLErrorDomain error -999 in iPhone 3.0 OS

which in turn refers to:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/6280-uiwebview-didfailloadwitherror-how-get-errors-code-list.html

Looks like you need a hack like this in the webView:didFailLoadWithError: delegate:

if ([error code] != NSURLErrorCancelled) {
    //show error alert, etc.
}

In essence what's happening is the delegate is getting a "cancelled" (-999) failure, that might be originated in javascript or perhaps even in a UIWebView bug.

With your code the webview doesn't show anything at all because you're using the very same UIWebView to display the error. If you had just NSLog'd the error you would have seen a failure, but then the page would have loaded just fine, giving you a hint that the failure is bogus.

这篇关于有没有人发现某些URL的UIWebView失败了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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