Top of the page
Close

This is a red error

This is a red error

This is a red error

iPhone internet connection check (WiFi, 3G, Edge) something like Reachability.h

Ondrej Rafaj on 2009.11.13 19:45:26

This class will allow you to check if user is connected to the internet or not

It is really easy to use and has just one function that returns YES or NO

Usage is really simple:


if ([Connection isConnected]) {  ...  }
else {  ...  }

Connection.m file:

//
//  Connection.m
//  iBlog
//
//  Created by Ondrej Rafaj on 12.11.09.
//  Copyright 2009 Home. All rights reserved.
//

#import "Connection.h"

@implementation Connection

+ (BOOL) isConnected {
	// Create zero addy
	struct sockaddr_in zeroAddress;
	bzero(&zeroAddress, sizeof(zeroAddress));
	zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    if (!didRetrieveFlags) {
        NSLog(@"Error. Could not recover network reachability flags");
		return NO;
    }
	BOOL isReachable = flags & kSCNetworkFlagsReachable;
	BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
	BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
	NSURL *testURL = [NSURL URLWithString:@"http://www.google.com/"];
	NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
	NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];
	return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}

@end

And this is the header Connection.h file:

//
//  Connection.h
//  iBlog
// 
//  Created by Ondrej Rafaj on 12.11.09.
//  Copyright 2009 Home. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>

@interface Connection : NSObject {

}

+ (BOOL) isConnected;

@end

And don't forget to include all the necessary frameworks ;) ... SystemConfiguration and libz.1.1.3.dylib

Ondrej Rafaj

Ondrej Rafaj

Technical director @ Fuerte International

Back to top Comment

Comments ... Why not to get involved!
HTML Comment Box is loading comments...

Old comments:

  • Carlos Vargas

    Carlos Vargas

    from 190.232.**.** @   

    Very useful and less confusing than the Reachability example in iphone Tutoirla
    Thanks

  • choise

    choise

    from 92.228.**.** @   

    Thanks thats a great piece of code. as mentioned, its very easy to handle instead of using the example code.

    one thing to say: your Class should be named something like "Web Connection" or similar, because "Connection" is a class of MessageUI Framework

Back to top

IE SUCKS, DON'T USE IT :) Digg this page