【问题标题】:Show error message is internet not available显示错误信息是互联网不可用
【发布时间】:2016-11-24 05:51:56
【问题描述】:

我正在加载webview 并使用SVProgressHUD 在页面加载时显示加载消息。

这是我的代码:

- (void)viewDidLoad{
    [super viewDidLoad];
    _infowebView.delegate = self;
    NSURL *url = [NSURL URLWithString:@"http://example.com"];
    [self.infowebView loadRequest:[NSURLRequest requestWithURL:url]];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD showWithStatus:@"Loading"];
}

如果没有可用的互联网连接,是否可以显示错误消息,如果可以,我该怎么做?

【问题讨论】:

  • 您可以在启动 Internet 所需代码之前检查 Internet 连接,使用 readability classes
  • 我只是使用了这个:- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [SVProgressHUD showInfoWithStatus:@"Internet Connection Unavailable"]; [UIApplication sharedApplication].networkActivityIndi​​catorVisible = NO;这是不好的做法吗?

标签: objective-c


【解决方案1】:

在加载页面之前,您可以使用可达性类检查互联网连接:

+(BOOL)IsInternet
{
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

使用方法:

if([className IsInternet]){
//Load the page
}else{
// Show message internet not available
}

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多