ですから、到達可能性の実装に関する投稿がここにたくさんあることは知っていますが、私の特定の質問に答えるものは見つかりません。
以下のコードを実装しているのですが、なぜかhostStatusがいつもNotReachableになってしまいます。
私の.hファイル:
#import <UIKit/UIKit.h>
#import "Reachability.h"
@class Reachability;
@interface ViewController : UIViewController<UINavigationControllerDelegate>{
Reachability* hostReachable;
Reachability* internetReachable;
}
@property (nonatomic, assign) BOOL wifiReachable;
@property (nonatomic, assign) BOOL networkReachable;
@property (nonatomic, assign) BOOL internetUsable;
私の.mファイル:
#import "ViewController.h"
#import "Reachability.h"
#import "Reachability.m"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
hostReachable = [Reachability reachabilityWithHostName:@"www.google.com"];
[internetReachable startNotifier];
[hostReachable startNotifier];
// now patiently wait for the notification
[self checkNetworkStatus:kReachabilityChangedNotification];
if(self.internetUsable==TRUE){
//DO STUFF
}
else{
[self internetAlert];
//DO OTHER STUFF
}
}
-(void) checkNetworkStatus:(NSNotification *)notice{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
NSLog(@"%u", hostStatus);
if(internetStatus==NotReachable){
NSLog(@"The internet is down.");
self.internetUsable=FALSE;
}
else{
if(internetStatus==ReachableViaWWAN){
NSLog(@"The internet is working via WWAN.");
self.networkReachable=TRUE;
self.internetUsable=TRUE;
}
else if (internetStatus==ReachableViaWiFi) {
NSLog(@"The internet is working via WIFI.");
self.wifiReachable=TRUE;
self.internetUsable=TRUE;
}
else{
self.networkReachable=FALSE;
self.wifiReachable=FALSE;
self.internetUsable=FALSE;
NSLog(@"The internet is NOT useable.");
}
}
if(self.internetUsable==TRUE)
{
if(hostStatus==NotReachable)
{
self.internetUsable=FALSE;
NSLog(@"Could not connect to the host");
}
}
}
私の推測では、hostStatus 接続が適切にチェックされる前に、CheckNetworkStatus メソッドに入っていると思います。
どんな助けでも本当に感謝します!