代码之家  ›  专栏  ›  技术社区  ›  runmad

在可达性方面需要一些帮助(2.0.3DDG)

  •  3
  • runmad  · 技术社区  · 16 年前

    当我的应用程序启动时,我会检查可访问性,因为我需要即时的互联网连接。不过,我的问题是,似乎没有立即确认 NetworkStatus ,这意味着在设置了可到达性之后,我会检查是否有连接,并返回没有连接,不管我实际上是在wifi/3g上,还是关闭了收音机。

    我可以确认我实际上正在获得一个互联网连接,因为应用程序启动完成后,会立即出现一个通知,然后记录“可通过WiFi访问”。

    我做错什么了?为什么它没有立即确认有效的互联网连接?

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        NetworkStatus netStatus = [hostReach currentReachabilityStatus];
        if (netStatus == NotReachable) {
            ErrorViewController *errorViewController = [[ErrorViewController alloc] initWithNibName:@"ErrorView" bundle:[NSBundle mainBundle]];
            [tabBarController.view removeFromSuperview];
            [window addSubview:[errorViewController view]];
            return;
        }
    }
    
    -(void)setupReachability {
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name:kReachabilityChangedNotification object: nil];
        hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
        [hostReach startNotifier];
    }
    
    -(void)reachabilityChanged:(NSNotification *)notification {
        Reachability* curReach = [notification object];
        NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        BOOL connectionRequired = [curReach connectionRequired];
        switch (netStatus)
        {
            case NotReachable:
            {
                [[NSUserDefaults standardUserDefaults] setInteger:kNOTREACHABLE forKey:kREACHABILITYSTATUS];
                NSLog(@"NotReachable");
                connectionRequired = NO;  
                break;
            }
    
            case ReachableViaWWAN:
            {
                [[NSUserDefaults standardUserDefaults] setInteger:kREACHABLEVIAWWAN forKey:kREACHABILITYSTATUS];
                NSLog(@"ReachableViaWWAN");
                break;
            }
            case ReachableViaWiFi:
            {
                [[NSUserDefaults standardUserDefaults] setInteger:kNOTREACHABLE forKey:kREACHABILITYSTATUS];
                NSLog(@"ReachableViaWiFi");
                break;
            }
        }
    }
    
    3 回复  |  直到 15 年前
        1
  •  4
  •   runmad    16 年前

    好吧,在我自己做了一些尝试之后,我实际上通过添加一行额外的代码让它工作了:

    -(void)setupReachability {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotificationV2 object:nil];
        hostReach = [[ReachabilityV2 reachabilityWithHostName:@"www.google.com"] retain];
        [hostReach connectionRequired]; // this line was added, and apparently forces a connection requirement..
        [hostReach startNotifier];
    }
    
        2
  •  2
  •   Community Mohan Dere    9 年前

    可达性示例代码为您提供异步回调/通知,以通知您可达性如何/何时发生了更改。为了使代码正常工作,您应该修改代码如下:

    - (void) applicationDidFinishLaunching:(UIApplication *)application {
    
     // setup reachability
        [self setupReachability];
     }
    

    然后在回调中,当您收到通知时,您会根据应用程序的需要作出反应。

    换句话说,您不能立即检查中的网络状态 applicationDidFinishLaunching() .如果您想这样做,那么您必须使用同步/阻塞方法,例如,您可以使用我的答案中提供的代码 this 问题。

        3
  •  1
  •   sach    15 年前

    必须将hostreach设置为类级变量。

    推荐文章