adsense

2011年6月28日星期二

check connection

之前有文章講用 NSURLCONNECTION 既 TIMEOUT 用來 CHECK 部 IPHONE 係未 CONNECTED

其實本身 APPLE 都出左個 CLASS,用來 CHECK
1. connected
2. wifi
3. mobile network connection

上網或係 XCODE 搵 reachability ,可以係 APPLE DOWNLOAD

個 PACKAGE 有 reachability.m , 同埋 reachability.h

要用個陣 抄入去係個 PROJECT 入边

另外要加個 framework 叫 SystemConfiguration.framework


以下係 EXAMPLE

我用黎係未 CONNECTED 可以參考 。。或者可以參考 reachability.m

TestConnection.h

#import

@class Reachability;

@interface TestConnectionViewController : UIViewController {


Reachability* internetReach;


}

@end


TestConnection.m


- (BOOL)checkInternetConnection{

BOOL connected;

internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];

if (netStatus == NotReachable) {
NSLog(@"Internet Connection Fail");
connected = NO;
}else {
NSLog(@"Internet Connection Success");
connected = YES;
}
return connected;
}


//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
BOOL internetConnection = [self checkInternetConnection];

if (internetConnection){
NSLog(@"connected");
}else {
NSLog(@"not connected");
}
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];


[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];



BOOL internetConnection = [self checkInternetConnection];

if (internetConnection){
NSLog(@"connected");
}else {
NSLog(@"not connected");
}


}

1 則留言:

chings 說...

netstaus

如果有 WIFI , 會講係 WIFI

無會 REUTRN 係用 WWAN (GPRS/3G)