早些时候我一直使用Tony Million的Reach-ability并且它总是很好。现在我也尝试了Apple Reach-ability但是每次我试图访问我总是得到互联网无法达到响应。
我还有什么要检查的吗?
这就是我做的事情
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
blockLabel.text = @"Block Says Reachable";
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
blockLabel.text = @"Block Says Unreachable";
});
};
[reach startNotifier];
答案 0 :(得分:4)
您还可以用来检查互联网连接, 首先,您需要导入文件
#import <SystemConfiguration/SCNetworkReachability.h>
#include <netinet/in.h>
<。>文件中的
- (BOOL) connectedToNetwork
{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable && !needsConnection) ? YES : NO;
}
如果此方法返回YES,则可以使用互联网连接。
这可以帮助你......
答案 1 :(得分:2)
如果遇到同样的问题,我打开Reachability.m文件并提交3g。
#if TARGET_OS_IPHONE
if(flags & kSCNetworkReachabilityFlagsIsWWAN)
{
// We're on 3G.
if(!self.reachableOnWWAN)
{
// We don't want to connect when on 3G.
connectionUP = NO;//Commit this line
}
}
#endif
希望这能解决您的问题。
#if TARGET_OS_IPHONE
if(flags & kSCNetworkReachabilityFlagsIsWWAN)
{
// We're on 3G.
if(!self.reachableOnWWAN)
{
// We don't want to connect when on 3G.
//connectionUP = NO;
}
}
#endif
答案 2 :(得分:1)
Reachability *r = [Reachability reachabilityWithHostName:@"www.Google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
blockLabel.text = @"Block Says Unreachable";
}
else
{
blockLabel.text = @"Block Says Reachable";
}
答案 3 :(得分:1)
旧的Reachability文件并不好。 Apple已更新其可访问性文件。
点击此处。
https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html
在此下载。
https://developer.apple.com/Library/ios/samplecode/Reachability/Reachability.zip
答案 4 :(得分:1)
Tony Million的Reachability也遇到了同样的问题:网络状态始终设置为NotReachable。我通过添加SystemConfiguration Framework来修复它
希望有所帮助