我使用這個庫:https://github.com/tonymillion/Reachability的iOS - 可達isReachable不工作
的問題是,有時當我檢查下面的表達式: [[[ReachabilityManager sharedManager] reachability] isReachable];
回報NO
時,應YES
。我知道有互聯網,因爲我在此期間調用Rest服務。
另外,如果我執行[Reachability reachabilityWithHostname:@"www.google.com"];
並要求方法isReachable
然後按我的預期工作。
我的測試很簡單,請嘗試在仿真器上,然後插上並拔下以太網電纜。
@implementation ReachabilityManager
#pragma mark -
#pragma mark Default Manager
+ (ReachabilityManager *)sharedManager {
static ReachabilityManager *_sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
});
return _sharedManager;
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
// Stop Notifier
if (_reachability) {
[_reachability stopNotifier];
}
}
#pragma mark -
#pragma mark Class Methods
+ (BOOL)isReachable {
return [[[ReachabilityManager sharedManager] reachability] isReachable];
}
+ (BOOL)isUnreachable {
return ![[[ReachabilityManager sharedManager] reachability] isReachable];
}
+ (BOOL)isReachableViaWWAN {
return [[[ReachabilityManager sharedManager] reachability] isReachableViaWWAN];
}
+ (BOOL)isReachableViaWiFi {
return [[[ReachabilityManager sharedManager] reachability] isReachableViaWiFi];
}
#pragma mark -
#pragma mark Private Initialization
- (id)init {
self = [super init];
if (self) {
// Initialize Reachability
self.reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
// Start Monitoring
[self.reachability startNotifier];
}
return self;
}
@end
嘗試使用apple的可達性類,您可以下載源代碼聽到https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html –
朋友你只是檢查主機可達性? –
我想測試互聯網連接。 – Ricardo