如何在 iphone 应用程序中检查网络状态? [英] How to check network status in iphone app?

查看:118
本文介绍了如何在 iphone 应用程序中检查网络状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中设置了一些方法来检查网络状态.

在我的 viewDidLoad 方法中,我调用了 initNetworkCheck:

[self initNetworkCheck];[超级viewDidLoad];如果(internetActive == NO){compose.enabled = NO;}

所以我想在启动时检查硬件是否有互联网连接.问题是它总是给我 NO 但当我登录时 internetActive 实际上是 YES.

//[[[[[[网络检查方法-(void)checkNetworkStatus:(NSNotification *)notice{NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];开关(互联网状态){案例不可到达:{self.internetActive = NO;休息;}案例 ReachableViaWiFi:{self.internetActive = 是;休息;}案例 ReachableViaWWAN:{self.internetActive = 是;休息;}}NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];开关(主机状态){案例不可到达:{self.hostActive = NO;休息;}案例 ReachableViaWiFi:{self.hostActive = 是;休息;}案例 ReachableViaWWAN:{self.hostActive = 是;休息;}}}-(void)initNetworkCheck{[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];internetReachable = [[ReachabilityreachabilityForInternetConnection] 保留];[internetReachable startNotifier];hostReachable = [[ReachabilityreachabilityWithHostName:@"www.google.com"] 保留];[hostReachable startNotifier];}//]]]]]]]

有什么想法吗?

解决方案

我建议使用 Apple 的 Reachability 类.此处是 Apple 的示例应用.><块引用>

可达性示例应用程序演示如何使用系统配置框架监控 iPhone 的网络状态或 iPod touch.特别地,它演示如何知道 IP 何时可以被路由以及何时交通通过无线广域路由网络 (WWAN) 接口,例如 EDGE或 3G.

I have setup some methods to check network status in my app.

In my viewDidLoad method I call initNetworkCheck:

[self initNetworkCheck];
[super viewDidLoad];
if(internetActive == NO){     
    compose.enabled = NO;
}

So I want to check on startup if the hardware has internet connection. The problem is it gives me always NO but internetActive is actually YES when I log it.

//[[[[[[network check methods
-(void)checkNetworkStatus:(NSNotification *)notice{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus) {
        case NotReachable:{
            self.internetActive = NO; 
            break;
        }
        case ReachableViaWiFi:{
            self.internetActive = YES;
            break;
        }
        case ReachableViaWWAN:{
            self.internetActive = YES;
            break;
        }
    }
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus){
        case NotReachable:{
            self.hostActive = NO;
            break;
        }
        case ReachableViaWiFi:{
            self.hostActive = YES;
            break;
        }
        case ReachableViaWWAN:{
            self.hostActive = YES;
            break;
        }
    }

}
-(void)initNetworkCheck{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    hostReachable = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
    [hostReachable startNotifier];
}
//]]]]]]

Any ideas?

解决方案

I suggest to make use of Apple's Reachability class. Here is a sample App by Apple.

The Reachability sample application demonstrates how to use the SystemConfiguration framework to monitor the network state of an iPhone or iPod touch. In particular, it demonstrates how to know when IP can be routed and when traffic will be routed through a Wireless Wide Area Network (WWAN) interface such as EDGE or 3G.

这篇关于如何在 iphone 应用程序中检查网络状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆