is there a simple piece of code testing if the internet is available on the iPhone - i know the reachability example app but that seems to be a great overhead. So i only like a network test (CFNetwork ?) who can check this - any help available?
It seems there is a bit of an issue with the above code and it may need to be modified depending upon your intended use. I put this code into my app (original code provided by Apple; thank you Apple) and it only worked when the iPhone was on a WiFi connection. I did some research and testing to come up with a solution.
There are a set of flags that can be used to determine connectivity (iPhone Dev Center). Not being entirely sure which to use, I set up a tester that would show me which flags are active when I am using different connection methods. This is something that I would recommend anyone to try so that you can get a better understanding of how it works, however this is the change I made that seemed to fix the issue:
If someone knows a better way or if im completely wrong here, please let me know.
The problem I am trying to research now is that once the iPhone has left the WiFi connection and gone on to the 3G or Edge networks, it wont go back onto the WiFi again even if I am in range of the signal. I have to exit my app and either wait or reinitialize it in the settings. Does anyone know a way around this?
What (I think) its doing is after it tests for reachability, it requests data from a www site using NSURLConnection, which makes the iPhone search for connections. Sorry for my non-technical explanations
EDIT: I changed the cache policy to not pull from the local cache so that it looks for the page every time. In my code, I also am not using apple.com as the request URL. I put up a simple html page on my server with the word 'true' in it and I am requesting that page for the connection testing. Keeping the request small might help the load time, even if you are doing nothing with the requested data. Even a 404 returns something.
This is very very good thread ,,atmost usefulllll..
but unable to understand , what does the zeroaddress contains..Any link on web would be help ful for understanding this..
Thank u all.
Last edited by mpramodjain; 02-21-2009 at 03:36 AM.
This is very very good thread ,,atmost usefulllll..
but unable to understand , what does the zeroaddress contains..Any link on web would be help ful for understanding this..
Thank u all.
The zero address is simply 0.0.0.0 - I in fact, chose to use apple.com to check for connectivity ...[self hostAvailable:@"www.apple.com"]....
The Reachability sample code is what you should be looking at here. Although the class it uses is almost 1000 lines long, it shows you how it all works. I'm about to start picking through it all for what I need
I like the idea of loading a small string from the web. I tested it on the simulator and the following code works fine if my internet is connected or not connected. I gave it a 2 second timeout...
EDIT: Tested it on the device as part of a much bigger app. Works as expected in flight mode / normal connection. If you want to increase the timeout perhaps it might be worth it, but it's working fine for me!
Last edited by ziophase; 12-15-2009 at 11:06 AM.
Reason: more info available
is there a simple piece of code testing if the internet is available on the iPhone - i know the reachability example app but that seems to be a great overhead. So i only like a network test (CFNetwork ?) who can check this - any help available?
Cheers :-)
Ralf
Two methods I tried. Reachability is still the best for me. It detect even changes from WifI to 3G to EDGE, etc...
Easier to use method is the UIWebView protocol itself. Hope the code below helps.
#pragma mark UIWebView delegate methods
- (void)webViewDidStartLoadUIWebView *)webView
{
// starting the load, show the activity indicator in the status bar
[myActivityIndicatorView startAnimating];
}
- (void)webViewUIWebView *)webView didFailLoadWithErrorNSError *)error
{
int myErrorCode;
[myActivityIndicatorView stopAnimating];
myErrorCode = [error code]; // myErrorCode = -999 if user cancel action, not network problem
if (ckErrorCode != -999)
{
// Your error handling code here
NSLog(@"UIWebView Load Failed-Error");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error!" message:@"Unable to connect to the website." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
}