简体   繁体   中英

Test Connection over Wifi only iOS

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];
}

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!

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;

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)

AFNetworking providing facility to check network connectivity -

AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
        // check your connectivity...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM