148iPhone: Checking Network Availability
Checking whether network is present and active. By way of: http://70.40.216.232/forums/viewtopic.php?f=21&t=425
UIApplication-Network.h
//
// UIApplication-Network.h
//
// SystemConfiguration.framework will need to be added to your project
//
// To use just call as a class function [UIApplication hasNetworkConnection]
//
#import
#import
@interface UIApplication (NetworkExtensions)
+(BOOL)hasActiveWiFiConnection; // fast wi-fi connection
+(BOOL)hasNetworkConnection; // any type of internet connection (edge, 3g, wi-fi)
@end
UIApplication-Network.m
//
// 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
Also, don't forget to add the SystemConfiguration.framework to the Frameworks folder in your application.
In case you get errors like that:
"_SCNetworkReachabilityGetFlags", referenced from:
+[UIApplication(NetworkExtensions) hasActiveWiFiConnection] in UIApplication-Network.o
+[UIApplication(NetworkExtensions) hasNetworkConnection] in UIApplication-Network.o
"_SCNetworkReachabilityCreateWithName", referenced from:
+[UIApplication(NetworkExtensions) hasActiveWiFiConnection] in UIApplication-Network.o
+[UIApplication(NetworkExtensions) hasNetworkConnection] in UIApplication-Network.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Build failed (2 errors)