[英]How to disable segue when there is no internet connection for iOS app?
为了检查Internet连接,我正在考虑通过AppDelegate更新:
为了停止搜寻,我正在考虑使用:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
我只想在用户点击UIButton时显示没有互联网连接的警报。
我到底应该做什么
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender
我已经在每个要执行互联网连接检查的ViewController的viewDidLoad方法下注册了通知
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
可达性已更改
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
}
在类接口中,声明:
@property (nonatomic) BOOL isReachable;
在您的reachabilityChanged方法中:
- (void)reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case ReachableViaWWAN:
{
self.isReachable = YES;
break;
}
case ReachableViaWiFi:
{
self.isReachable = YES;
break;
}
case NotReachable:
{
self.isReachable = NO;
break;
}
}
}
然后,使用shouldPerformSegueWithIdentifier方法检查是否已连接到Internet。
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([identifier isEqualToString:YOUR_IDENTIFIER]) {
if (!self.isReachable) {
return NO;
}
}
return YES;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.