1

I have a method implemented thats tests the internet connection in my iOS app. The problem is, I only want the method to test the internet connection over wifi, not cellular. How do I do that? :-) PS I'm using the latest version of Xcode & iOS 6.

NSURL *scriptUrl = [NSURL URLWithString:@"http://10.247.245.87/stores/test.html"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data != nil){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Success"
                                                   message: @"You're connected to the server"
                                                  delegate: self
                                         cancelButtonTitle: @"Close"
                                         otherButtonTitles:nil];

//Show Alert On The View
[alert show];
}

else {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Connection Failed"
                                                   message: @"Please connect to network and try again"
                                                  delegate: self
                                         cancelButtonTitle: @"Close"
                                         otherButtonTitles:nil];

//Show Alert On The View
[alert show];
}
Charles Vincent
  • 229
  • 4
  • 15

4 Answers4

2

Have you looked into the Reachability class?

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

It will probably provide you with a lot more flexibility and why roll your own if Apple has already done the work :) Hope this helps!

jerrylroberts
  • 3,379
  • 21
  • 22
  • I tried that but it looks as though its not compatible with the newer version of Xcode. All it did was give me compiler errors :-( – Charles Vincent Oct 26 '12 at 12:48
  • I'm using it. Now if you have an ARC project, you may have to go in and do some tweaking. – jerrylroberts Oct 26 '12 at 13:00
  • Im getting these errors. ANy thoughts? `Undefined symbols for architecture i386: "_OBJC_CLASS_$_Reachability", referenced from: objc-class-ref in TCViewController.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)` – Charles Vincent Oct 26 '12 at 13:22
  • 1
    @CharlesVincent You can disable ARC on the reachability classes by adding the compiler flag `-fno-objc-arc` to reachability.m in your build phases. Or, you could download [this version](https://github.com/tonymillion/Reachability) of reachability modified for ARC compliance. – Mick MacCallum Oct 26 '12 at 13:23
  • @NSPostWhenIdle Thanks for the tip! Appreciated! – Charles Vincent Oct 26 '12 at 13:35
2

Check Tony Million Reachability class

There must be a isReachableViaWiFi method you can check wifi only

// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// tell the reachability that we DONT want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = NO;
u.gen
  • 4,757
  • 4
  • 39
  • 76
0

Have you looked at this similar question:

How to check for an active Internet connection on iOS or OSX?

The highest up voted question on that gives a github link to a neat workaround and shows you if the internet connection is active via wifi or WWAN (cellular)

Community
  • 1
  • 1
MCKapur
  • 9,029
  • 9
  • 56
  • 99
0

AFNetworking providing facility to check network connectivity -

AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
        // check your connectivity...
    }
Prakash Raj
  • 1,935
  • 1
  • 14
  • 13