dear Stackover community,
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 :-)
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]; }
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 :-)
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.
Thanks in advance
The delegate method is:
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
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 ?"
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");
}
}
TRY ...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:[error localizedString
]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
Related
I use this code to check for internet when the app opens for the first time:
//No Internet Connection error code
-(void)webView:(UIWebView *)webVIEW didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Internet Connection!" message:#"In order to use this app you need an active Internet connection!" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil, nil];
[alert show];
}
//Close app from Alert View
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
exit(0);
}
I have a UIWebView app that has a jQuery .click() function. Is there a way to check for Internet again once the user clicks that button?
Take a look at Apple's sample code. The Reachability project shows how to detect a connection
http://developer.apple.com/iphone/library/samplecode/Reachability/index.html
Keep in mind, a click doesn't necessarily request more resources over a network connection.
UIWebViewDelegate has a webView:shouldStartLoadWithRequest:navigationType selector that will be your friend here. To trigger it whenever you want (on click or other actions), set the source of a hidden iframe to some dummy value, and check for that value in your delegate method (which will run your test and return NO.
I recommend not exiting when there is no connection though, since users may see it as a crash, especially if their connection loss is only temporary (going through a tunnel). Also, repeatedly testing for connection may slow down your app unnecessarily, and incur more data charges for the user.
you could do like that:
in function click() you should add this code:
window.location = "fake://myApp/checkForInternet";
in your Objective-C code add this one:
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *link = request.URL.absoluteString;
if ([link isEqualToString:#"fake://myApp/checkForInternet"])
{
// check for Reachability here
return NO;
}
return YES;
}
note that in this example -webView:shouldStartLoad... is called only once per event in webView. I can provide more complexed code if you want to do checking twice or more in one javascript function at one event
read about checking for reachability you can, as #Rajneesh071 said, in Apple documentation or here is the project on GitHub
If you're using Apple's Reachability code or similar then you want the currentReachabilityStatus method; otherwise call the underlying SCNetworkReachabilityGetFlags function
I'm in the process of submitting my app to the App Store, but I read that I must notify the user if the internet connection is down when my app needs it. The Apple page mentioned Reachability as well. Currently, though, I'm using the UIWebView delegate method didFailLoadWithError...
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error Loading" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
...and it's working fine. My question is, will my app be rejected for not using Reachability to do this, or is it fine doing what I am currently doing?
Thanks in advance.
No, you're perfectly ok using didFailLoadWithError:.
Reachability class could be used to check if host is up (or internet connection at all) before even trying to load some page. But it is not neccessery, as long as you handle the possible errors - which obviously you do.
EDIT:
It is still a good practice to know wheather you will be able to reach a certain host or not. You could even modify GUI for each case (instead of just reporting an error). But this can always be done in update :)
I am new to iphone..
Actually i wanted to know is it possible to put an UIAlertView after the completion of NSLog as i wanted to check whether the statement really works or not as i cant check NSLog in the iphone.
Please suggest me whether it is possible and how?
NSLog will still work on iPhone..
You can only see it in the console if you have attached the device to your mac..and then use the app.
Anyways.
You can show your AlertView after log like this.( This Nslog is an example..replace with what is your actual log)
NSLog(#" Image Loaded");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log" message:#"Image Loaded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
[alert release];
If the NSLog statement works, it'll show up in the XCode console. So if you debug the app on your phone via XCode, you should see the NSLog. Or did you have something else on your mind ?
I have a code to detect if I have internet connection or not, and sometimes it doesn't work well. Here's the code:
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Error" message:#"Couldn't connect to the Internet. Please check your connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
The problem is that sometimes a web doesn't load for other things and not because it has a bad connection. I hope that there's a better way to check the WiFi. Any idea?
I suggest you follow the link and read the best marked answer given in there. It worked perfectly with me.
How to check for an active Internet connection on iOS or OSX?
I need to protect my code against possible errors. If they arise then there's no point to run the app further, so I need to bring to the user some message and then exit the app. So, I'm checking the conditions and then bringing alert:
if (someError){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"No database file exist. App will close now." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
And in the delegate method I'm closing the app using NSAssert:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSAssert(0, #"closing");
}
}
Also, I have included delegate protocol in header. However, the app just brings alert but after pressing OK it just freezes, and I'm getting some message "CoreAnimation: ignoring exception: closing". What am I missing or what other options exits?
You should not do that, it is against Apple HIG (Human Interface Guidelines):
iPhone applications should never quit programmatically because doing so looks like a crash to the user.
It is always better to provide some kind of feedback to the user about the error that occured and provide a way to restart the process without restarting the app. However, if you really, really want, you can use
exit(0);