简体   繁体   English

我如何知道页面未能加载到UIWebView中而不是被用户“停止”?

[英]How do I know when a page has failed to load in UIWebView as opposed to being “Stopped” by the user?

I have a UIWebView within my application which I'm using to provide web browsing functionality. 我的应用程序中有一个UIWebView,用于提供Web浏览功能。

I have a button on my toolbar which calls 我的工具栏上有一个按钮

-(void)stopLoading;

on the UIWebView. 在UIWebView上。 The problem with this is that when this method is sent to the web view, the delegate receives the 问题在于,当此方法发送到Web视图时,委托会收到

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

message. 信息。 The error accompanying this call is: Operation could not be completed. 伴随此调用的错误是: 操作无法完成。 (NSURLErrorDomain error -999.) (NSURLErrorDomain错误-999。)

I also use the didFailLoad message to tell if the page actually failed to load - and in that case, I display an html formatted message to user to communicate this failure to them. 我还使用didFailLoad消息来判断页面是否实际加载失败-在这种情况下,我会向用户显示html格式的消息,以向他们传达此失败信息。

So the problem is that if the user hits the stop button, the error page is shown instead of just showing whatever parts of the page loaded before being stopped. 因此,问题在于,如果用户单击“停止”按钮,则会显示错误页面,而不是仅显示在停止之前加载的页面的任何部分。

I had hoped that NSError.h contained an enum of error codes that I could compare against - but it seems like I'll have to make my own based on my observations of the error codes that come out in my logs - which is less than ideal, since they could change in the future... 我曾希望NSError.h包含一个可以与之进行比较的错误代码枚举-但似乎我必须根据对日志中出现的错误代码的观察来自行编写-小于理想的,因为它们将来可能会改变...

Any suggestions would be great, thanks. 任何建议都很好,谢谢。

这些错误代码在Foundation.framework的NSURLError.h中定义

The best way I could find to solve this was to create an enum using the error codes given by the webView. 我能找到解决此问题的最佳方法是使用webView给出的错误代码创建一个枚举。

It doesn't look like they're going to change, but if they do, well - ho hum. 看起来他们并不会改变,但是如果这样做,嗯-哼。

So now, in my failed-to-load method I simply compare the error code from the error object with my enum, and I can see what happened - looks kinda like this: 因此,现在,在失败加载方法中,我仅将错误对象中的错误代码与枚举进行比较,就可以看到发生了什么-看起来像这样:

typedef enum {  
    BrowserErrorHostNotFound = -1003,
    BrowserErrorOperationNotCompleted = -999,
    BrowserErrorNoInternetConnection = -1009
} BrowserErrorCode;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM