Web视图中用于连接检查的简单警报视图 [英] simple alert view for connection check in a web view

查看:34
本文介绍了Web视图中用于连接检查的简单警报视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的Stackover社区

dear Stackover community,

近一周来,我一直在为必须进行连接检查而苦苦挣扎我的网络视图,因此我可以幸免于Apple的App审核过程:-)

Nearly a week I'm struggling around with the fact that I must have a connection check for my web view so I'm surviving the App review process by Apple :-)

我知道我可以使用Reachablity示例,但是对我来说,作为一个初学者,我会说处理这样的代码并不明智.

I know that I can use the Reachablity sample but for me as a beginner I would say its not smart to deal with such a code.

我发现使用简单的警报和如下代码可以使它变得更简单:

I found out that it gets a bit simpler with a simple alert and code like this:

    -(void)webview:(UIWebView *)webview didFailLoadWithError:(NSError *)error {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:@"Can't connect. Please check your internet Connection"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show]; }

我的问题是我在Webviewcontroller的.m中实现了此代码,并将委托连接到Filesowner,但该应用对我来说不是很高兴向我显示错误消息:-)

My problem is that I implemented this code into the .m of my Webviewcontroller and connected the delegate to the Filesowner but the App won't be that nice to me to show me the error message :-)

此刻,我正在使用启用了ARC和情节提要的Xcode 4.2.1.

At the moment I'm using Xcode 4.2.1 with ARC enabled and storyboards.

任何人都可以给我逐步指导,以了解如何进行这项工作吗?这可能是我需要为我的应用程序提供的最后一个代码.

Would anybody please give me a step by step guide how to get this work? Its probably the last code I need for my App.

预先感谢

推荐答案

委托方法是:

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

请注意,它是"-(void)webView ",而不是"-(void)webview ".只需将视图"中的"v"大写即可.

Note it's "-(void)webView" not "-(void)webview". Just capitalize the "v" in "view" and it will work.

要回答您随后的问题:现在,当我停止加载页面时,也会出现错误消息.还有其他提示吗?"

To answer your subsequent question:"now when i stop the loading of the page the error message comes too. Is there another tip ?"

如您所见,将 NSError 传递给您的函数.该错误包含有关失败的信息.您可以通过添加以下代码来查看该信息: NSLog(@"Epic失败,错误:%@",error); (添加了"Epic"以产生显着效果).记录该错误时,您会看到列出的 code 属性".通过一些实验,您将看到此代码属性根据错误的类型而变化.对于连接错误,此代码似乎等于 -1009 .所有可能的错误都是此处在"URL加载系统错误代码"下进行了枚举.最重要的是,由于它们是在枚举中定义的,因此您不必记住很多.您可以使用:

As you can see an NSError is passed to your function. That error contains information about the failure. You can see that information by adding: NSLog(@"Epic fail with error:%@",error); ('Epic' added for dramatic effect). When you log that error you will see a code 'property' listed. Through some experimentation you will see that this code property changes based on the type of error. It seems that this code will equal -1009 for a connection error. All of the possible errors are enumerated here under "URL Loading System Error Codes". Best of all since they are defined in an enumeration you don't have to remember much of that. You can just use:

-(void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
    if (error.code == NSURLErrorNotConnectedToInternet){
        NSLog(@"You are not connected");
    }
}

这篇关于Web视图中用于连接检查的简单警报视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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