如何在没有互联网连接的情况下检测到。或者如果连接超时。 我正在使用NSURLConnection类。
答案 0 :(得分:1)
Apple为您提供Reachability类,将其添加到ypur项目中。导入Reachability.h和Reachability.m文件。添加systemConfiguration框架工作。并在代码中添加以下代码段。
Reachability *internetReachableinst;
internetReachableinst= [Reachability reachabilityWithHostName:@"www.google.com"];
internetReachableinst.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Internet is connected");
});
};
//Internet is not reachable
internetReachableinst.unreachableBlock = ^(Reachability*reach)
{
//Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"No internet");
});
};
[internetReachableinst startNotifier];
答案 1 :(得分:0)
检查互联网连接的最简单方法是使用以下代码。
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
NSString *result = [[NSString alloc] init];
result = ( URLString != NULL ) ? @"Yes" : @"No";
NSLog(@"Internet connection availability : %@", result);
但此代码必须依赖google.com的可用性