簡體   English   中英

iOS應用程序沒有互聯網連接時如何禁用segue?

[英]How to disable segue when there is no internet connection for iOS app?

為了檢查Internet連接,我正在考慮通過AppDelegate更新:

  1. SystemConfiguration.framework
  2. 可達性

為了停止搜尋,我正在考慮使用:

- (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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM