2017-10-16 41 views
0

我是新来的目标c。我发现了很多方法来检查互联网连接,如可达性测试,使用AF网络等。这是最好的方法吗?任何帮助?哪一个是最好的方法来检查目标中的网络可达性c

+0

可以使用可达性。 –

+0

下载可达性类 https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html –

回答

0

我总是比较喜欢使用可达性https://github.com/tonymillion/Reachability。 GitHub上的描述对于你为什么要使用它是如此清楚 - >“这是苹果Reachability类的直接替代品,它与ARC兼容,它使用新的GCD方法来通知网络接口更改。”

Reachability *internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; 

// Internet is reachable 
internetReachableFoo.reachableBlock = ^(Reachability*reach) 
{ 
    // Update the UI on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     //do something 
     NSLog(@"REACHABLE!"); 

     } error:^(NSError * _Nullable error) { 
      NSLog(@"Error:%@ ", [error localizedDescription]); 
     }]; 
    }); 
}; 

// Internet is not reachable 
internetReachableFoo.unreachableBlock = ^(Reachability*reach) 
{ 
    // Update the UI on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     //do something 
     NSLog(@"UNREACHABLE!"); 

     }); 
}; 

[internetReachableFoo startNotifier]; 

好运:)

相关问题