0

我试图让我的应用程序确定用户是否有互联网连接以及他们拥有什么类型的连接。我已经导入了 SystemConnection 框架和 Reachability .h 和 .m 文件。

在我的 viewController.h 中,我有以下内容:

#import "Reachability.h"

Reachability* reachability;

并在 vc.m

//notification for network status change
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:nil];

[reachability startNotifier];


//check connectivity
[self checkConnectivity];

在检查连接:

- (void) checkConnectivity {

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        NSLog(@"no connection");
    } else if (remoteHostStatus == ReachableViaWiFi) {
        NSLog(@"wifi");
    } else if (remoteHostStatus == ReachableViaWWAN) {
        NSLog(@"cell");
    }

}

这在启动时工作正常。我记录了进度,它按预期返回:

2013-07-29 09:35:17.084 OAI_Project_Template[6095:c07] not connected - network change
2013-07-29 09:35:17.093 OAI_Project_Template[6095:c07] wifi- check connectity 

但是,如果我打开或关闭我的 wifi 连接,则永远不会调用 handleNetworkChange。

- (void) handleNetworkChange : (NSNotification*) notification {

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
    isConnected = NO;
    NSLog(@"not connected - network change");
    } else if (remoteHostStatus == ReachableViaWiFi) {
    NSLog(@"wifi - network change");
    } else if (remoteHostStatus == ReachableViaWWAN) {
    NSLog(@"cell");
    }
}

我环顾四周,看到了很多类似的问题,但解决方案似乎都是我所拥有的。

如果这很重要,我正在模拟器中工作。任何帮助,将不胜感激。

4

2 回答 2

0

https://github.com/tonymillion/Reachability是苹果可达性类的自定义替代品

于 2013-07-29T16:29:04.340 回答
0

这是我必须检测网络的内容。基本上,我只需要检测应用程序是否可以访问互联网中的服务器,一旦发布通知,检查访问权限并根据访问权限进行处理。确保仅在应用程序处于前台时才注册可达性。

// Called from applicationDidBecomeActive

- (void) startMonitoring
{
    //Register for change in reachability   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    // Setup a target server to detect if a host on the internet can be accessed    . For example www.apple.com. Defined as instance variable
        hostReach = [Reachability reachabilityWithHostName: @"www.apple.com"];
        [hostReach startNotifier];
}

- (void)reachabilityChanged:(NSNotification *)note
{
    //NSLog(@"%s %@", __FUNCTION__, note);
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

    [self updateInterfaceWithReachability: curReach];

}
- (void)updateInterfaceWithReachability: (Reachability*) curReach

{


// Check if the host site is online
NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
switch (hostStatus)
{
    case NotReachable:
    {
        NSLog(@"%s No access - ", __FUNCTION__);

        break;
    }
    case ReachableViaWiFi:
    {
        // Check for LAN switch
        NSLog(@"%s WIFI Available - ", __FUNCTION__);
        break;
    }
    case ReachableViaWWAN:
    {
        // Disable LAN switch
        NSLog(@"%s WIFI NOT Available ", __FUNCTION__);

        break;
    }
 }
}
于 2013-07-29T17:12:46.597 回答