私はApples Reachabilityクラスを利用していますが、それをメインスレッドに保持すると機能します(悪いアプローチ)。代わりに別のスレッドに移動すると、通知は呼び出されません。
didFinishLaunchingWithOptions
私は以下を呼び出します:
[NSThread detachNewThreadSelector:@selector(checkConnection) toTarget:self withObject: nil];
checkConnection は次のようになります。
-(void)checkConnection {
//Test for Internet Connection
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
Reachability *r = [[Reachability reachabilityWithHostName:@"appspot.com"] retain];
[r updateReachability:appDelegate.reachability];
[r startNotifier];
[pool release];
}
そして、reachabilityChanged は次のようになります。
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateReachability: curReach];
}
最後に updateReachability は次のようになります。
- (void)updateReachability:(Reachability *)curReach {
NetworkStatus internetStatus = [curReach currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
NSLog(@"No net");
} else {
NSLog(@"Lots of net");
}}
reachabilityChanged
が呼び出されない理由を理解するのを手伝ってくれることを願っています。
乾杯...