2016-11-24 31 views
0

我正在加載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"]; 
} 

Is it possible to show an error message if there isn't an internet connection available, if so how would i do this?

+0

重複的http://stackoverflow.com/questions/477852/checking-for-internet-connectivity-in-objective-c – Darshana

+0

你可以檢查互聯網連接,然後啓動互聯網所需的代碼,使用[可讀性類](https:///developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html)。 – vaibhav

+0

我只是用這個: - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)錯誤{SVProgressHUD showInfoWithStatus:@「Internet Connection Unavailable」]; [UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO; }這是不好的做法? – user1419810

回答

0

加載您可以使用可達性類檢查互聯網連接的頁面之前:

+(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 
}