Xcode - Adding network error alert code into my app?

淺唱寂寞╮ 提交于 2019-12-24 03:39:22

问题


Hi I want to add network error code into my iPhone app, heres what I have got up to now:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The page couldn't load, please check your internet connection and reopen the app." delegate:self
    cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

I have linked the webview in question in interface builder with delegate. It works on simulator quite well, but when I run it on my iPhone I realised a few things. It only works upon opening up the app the first time when theres no internet connection, if you open the app later with no internet connection and it's still in the multitasking bar it doesn't work. It doesn't work if you lose internet connection while using the app and try to load another page, and that leads on to that currently I have only added this code for 1 uiwebview, while I have 4 otherwise if the error does show up, it shows up 4 times. I need to work out a way to link it to all uiwebviews, but it will only show up once if there is no connection.

Thanks for help in advance.


回答1:


Check out Apple's Reachability sample code.




回答2:


webView didFailLoadWithError: ---- to fire this everytime you come to webview write ur code in the viewWillAppear.... or try something like this in viewWillAppear ... [webView reload];

Njoy.... !



来源:https://stackoverflow.com/questions/4773711/xcode-adding-network-error-alert-code-into-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!