0
嗨,我在Apple的可达性代码中遇到了一些问题。 我发现,即使设备正确连接到互联网,最初可达性代码会发送1个虚假通知(Networkstatus = NotReachable),然后发送几个正确的通知(Networkstatus = ReachableViaWiFi)。 因此,当我在显示UIAlertView时收到“NotReachable”通知时,即使设备连接到互联网,应用程序仍会输出uialertview,通知用户设备未连接。Reachability + UIAlertView + false-positive
反正有避免这种不便吗?
任何帮助将非常感激。
这是我的代码:
在我的.h文件:
@property (nonatomic, retain) Reachability *hostReach;
在我的.m文件:
- (void)viewDidLoad
{
self.hostReach = [Reachability reachabilityWithHostname:@"www.google.com"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[_hostReach startNotifier];
NetworkStatus netStatus = [self.hostReach currentReachabilityStatus];
if(netStatus == NotReachable && _alertShowing==NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"No internet connection found"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
_alertShowing = YES;
[alert show];
}
...
}
-(void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(netStatus == NotReachable && _alertShowing==NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"No internet connection found"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
_alertShowing = YES;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
_alertShowing = NO;
}
非常感谢@EugeneK,我认为解决了它。但是Google会因为什么原因阻止我? Ps我使用的是reachabilityWithHostname:@“www.google.com”,因为在Apple的示例代码中,他们使用的是reachabilityWithHostname:@“www.apple.com”,我认为谷歌会拥有更可靠的服务器。 – puntotuning
那么,如果谷歌认为你或你的用户试图通过发送大量数据包到他们的服务器来DDOS他们,你可能会考虑三次提出这样的问题。 – CodaFi
好吧,我了解你的观点@CodaFi。但是如果是这样的话,为什么苹果使用reachabilityWithHostname:@“www.apple.com在他们的可达性示例代码中? – puntotuning