UIApplication extension for checking for network connectivit

sample code that might be beneficial to others...

UIApplication extension for checking for network connectivit

Postby HardyMacia » Thu Jan 29, 2009 2:57 am

I think Apple should have made the ability to check for the network connection much easier for developers. Here’s my shot at it…

The following two files UIApplication-Network.h and UIApplication-Network.h extend the UIApplication class.

To use the following in your app you need to include the SystemConfiguration.framework, then to check for internet connectivity is as simple as [UIApplication hasNetworkConnection]; or if you need a WiFi connection [UIApplication hasActiveWiFiConnection];

The networking checking code is from Reachabilty or a posting on these forums, I just extended the UIApplication.

UIApplication-Network.h
Code: Select all
//
//  UIApplication-Network.h
//
//  SystemConfiguration.framework will need to be added to your project
//
//  To use just call as a class function [UIApplication hasNetworkConnection]
//

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SCNetworkReachability.h>

@interface UIApplication (NetworkExtensions)

+(BOOL)hasActiveWiFiConnection;     // fast wi-fi connection
+(BOOL)hasNetworkConnection;     // any type of internet connection (edge, 3g, wi-fi)

@end


UIApplication-Network.m
Code: Select all
//
//  UIApplication-Network.m
//

#import "UIApplication-Network.h"

@implementation UIApplication (NetworkExtensions)


#define ReachableViaWiFiNetwork          2
#define ReachableDirectWWAN               (1 << 18)
// fast wi-fi connection
+(BOOL)hasActiveWiFiConnection
{
     SCNetworkReachabilityFlags     flags;
     SCNetworkReachabilityRef     reachabilityRef;
     BOOL                              gotFlags;
   
     reachabilityRef = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(),[@"www.apple.com" UTF8String]);

     gotFlags = SCNetworkReachabilityGetFlags(reachabilityRef, &flags);
     CFRelease(reachabilityRef);
   
     if (!gotFlags)
     {
          return NO;
     }
   
     if( flags & ReachableDirectWWAN )
     {
          return NO;
     }
   
     if( flags & ReachableViaWiFiNetwork )
     {
          return YES;
     }
   
     return NO;
}

// any type of internet connection (edge, 3g, wi-fi)
+(BOOL)hasNetworkConnection;
{
    SCNetworkReachabilityFlags     flags;
    SCNetworkReachabilityRef     reachabilityRef;
    BOOL                              gotFlags;
   
    reachabilityRef     = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"www.apple.com" UTF8String]);
    gotFlags          = SCNetworkReachabilityGetFlags(reachabilityRef, &flags);
    CFRelease(reachabilityRef);
   
    if (!gotFlags || (flags == 0) )
    {
        return NO;
    }
   
    return YES;
}

@end
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Return to Source Code Snippets

Who is online

Users browsing this forum: No registered users