0

Possible Duplicate:
How to check for an active Internet Connection on iPhone SDK?

I am worried if this code will be approved by apple for checking whether an internet connection exists.

Now is this code is alright, or am I still missing something? Edited code:

Reachability* reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");}
else if (remoteHostStatus == ReachableViaWWAN) { NSLog(@"reachable via wwan");}
else if (remoteHostStatus == ReachableViaWiFi) { NSLog(@"reachable via wifi");}
Community
  • 1
  • 1
prajakta
  • 673
  • 6
  • 26
  • Sure, it'll be approved. It's just not going to do quite what you expect. – Jonathan Grynspan Apr 22 '11 at 13:57
  • NSString connected would not be equal to NULL in your code. Why are you using wait ? wait blocks current thread and if you are running this on your main thread you are blocking it. Also this is not checking Internet availability between NSString* connected and if(connected == NULL) – 0x8badf00d Apr 22 '11 at 14:00
  • 3
    Internet is not there: someone stole it. – Albireo Apr 22 '11 at 14:01
  • Checking whether `connected` is `nil` is the wrong way, you should be checking `requestObj`. – sudo rm -rf Apr 22 '11 at 14:04
  • You edit is fine if you really wish to use reachability. However, it seems like it's overkill just for one simple load of a page. See the code I added to my question below for your original question modified to work correctly. Whichever way you chose will be fine with Apple (as long as it actually works). :) – sudo rm -rf Apr 22 '11 at 15:11
  • your code never show alert box if no internet , i don know why :( – prajakta Apr 22 '11 at 15:33
  • @sudo my requestObj is never nil even if no internet :( – prajakta Apr 22 '11 at 15:52

4 Answers4

1

You should look into Apple's Reachability, a class for detecting the presence of internet.

However, Apple's class was poorly written, so others took it upon themselves to clean it up. This class was written by Andrew Donoho as a drop-in replacement for Apple’s Reachability class:

http://blog.ddg.com/?p=24


If you really want to use your own methods, here's an improved version:

NSURL        *requestUrl = [NSURL URLWithString:@"http://www.something.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
NSData       *loadTest   = [NSData dataWithContentsOfURL:requestUrl];

if (loadTest == nil) {
    NSLog(@"No internet");
} else {
    [localwebView loadRequest:requestObj];
    [localwebView release];
}
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • hi sudo what you think , my code will be approved or not?? – prajakta Apr 22 '11 at 13:59
  • If you're insistent on using your code, first of all you should ditch `wait(20);`. There's no need to wait, as soon as the URL is loaded (or not loaded if there's no internet) it will move to the next line. Also, you're doing it wrongly. If you want to see if you have internet you could just do this: `NSURL *requestUrl = [NSURL URLWithString:@"http://www.ggg.php"]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];` , and check whether `requestObj` is nill instead of what you're doing with that whole string business. – sudo rm -rf Apr 22 '11 at 14:03
  • based on your exp what you think is this code ok with them else i will change it to reachablity one ?? let me know – prajakta Apr 22 '11 at 14:12
  • Currently your code isn't going to work. I'll post an example in my answer for you. – sudo rm -rf Apr 22 '11 at 14:15
  • ya i mean if i follow your code then is there any issue with apple team?? – prajakta Apr 22 '11 at 14:16
  • If you use Reachability code, you'll have no trouble. If you use the code I just added to my answer, you shouldn't have trouble either. – sudo rm -rf Apr 22 '11 at 14:18
  • i added the reachabilty code , now is that correct??? plz see updated code above thanks – prajakta Apr 22 '11 at 14:42
  • hey sudo , your code is not working if no internet ( that alert code is not working ) – prajakta Apr 22 '11 at 15:17
  • I messed that up. Try now. :) – sudo rm -rf Apr 22 '11 at 16:04
  • ya now working , so i am not using reachabilty , i stick to ur code, but plz assure me again as apple wont reject this code since i am not using their reachabilty , Thanks i am quite tensed – prajakta Apr 22 '11 at 16:10
  • I use something similar to this in my code and Apple doesn't make a peep. You're fine. :) If my code helped you don't forget to mark it as answer. – sudo rm -rf Apr 22 '11 at 17:13
0

You should use Reachability api by Apple to check for internet connection. Here is the sample code:

http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/ReadMe_txt.html

Hetal Vora
  • 3,341
  • 2
  • 28
  • 53
0

Have a look at Apple's Reachability example. I use their code to get notifications when the reachability status changes. https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

mmoris
  • 9,970
  • 3
  • 18
  • 12
0

A better way to achieve this would be to use asynchronous requests. That way you dont block the main thread and wont risk getting the app killed by the OS.

If there is no internet you will get an instance callback on the delegate method: connection:n didFailWithError:

Reachability is a lazy way of doing it as it effectively makes another request to test if there is internet. This means for the case when there is internet available you will be making two requests needlessly.

stringWithContentsOfURL is a synchronous call. Use initWithRequest:delegate: to move to asynchronous requests.

Alternatively you may wint to look at ASI. They are a nice wrapper around the NSURL methods.

Robert
  • 37,670
  • 37
  • 171
  • 213