I'm writing an app for a college that will likely be used on campus. The college's wifi requires credentials to access the internet through a web page (such as AT&T hotspots). I would like my app to detect whether it's 'connected' to the internet or not. In the past, I've seen other applications redirect to Safari so the user can authenticate and then go back to the application. Does anyone know how to detect this sort of thing without simply trying to grab NSData from a connection (such as google.com) and then assuming if no data is grabbed this is the issue?
-
I'm also unsure if Reachability would handle this situation and am cautious of just including apple's example project' in here rather using something more proper like the Security Framework. I'm not even sure how I would apply this and would be very surprised if there isn't something built into NSConnection or something along those lines that would handle it. My question is also way more specific than the noted question. – TheCodingArt Apr 03 '13 at 23:59
-
You said you want your app to detect whether it's connected. That's the exact purpose of Reachability. The example project just shows how to wrap a C API in Objective-C. – Jim Puls Apr 04 '13 at 00:59
3 Answers
iOS automatically brings up a web view when you are trying to connect to a network that has a captive portal. To make sure you are connected and authenticated in your app, you should set UIRequiresPersistentWiFi
in your Info.plist
.
EDIT: My answer above is only for apps that require an internet connection. If you're just checking whether you are connected and authenticated, I believe you just have to use Reachability
and check that you are ReachableViaWiFi
. (I believe SystemConfiguration
will not say you are reachable via Wi-Fi if you are not authenticated.)
If you are looking to handle the captive network authentication in your app instead of in the iOS default web view, you can use the CaptiveNetwork API.

- 5,345
- 4
- 32
- 49
Since you don't want to use a NSData way (I don't like grabbing NSData either as well as using reachability) Here is what I came up with that is more lightweight because it just checks the HEAD response :) :
- (BOOL)connectedToInternet
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.google.com/"]];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response error: NULL];
return ([response statusCode] == 200) ? YES : NO;
}
- (void)yourMethod
{
if([self connectedToInternet] == NO)
{
// Not connected to the internet
}
else
{
// Connected to the internet
}
}

- 5,862
- 10
- 58
- 91
If you don't want to use Reachability you could initiate a NSURLConnection to a random website while on the campus wifi and check for an authentication challenge.
Set up the NSURLConnection
:
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
Implement the auth challange delegate method:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"I'm being challenged.");
}
Then do what you want after the challenge.
Without going and looking at Reachability, if you try to use it to reach a host and it is presented with an auth challenge, it may return that there is no connection because it couldn't reach the specified host. Again, not 100% sure if this statement is accurate.

- 8,568
- 12
- 50
- 85