gpt4 book ai didi

iphone - 在 Reachability (2.0.3ddg) 方面需要一些帮助

转载 作者:搜寻专家 更新时间:2023-10-30 19:59:16 24 4
gpt4 key购买 nike

当我的应用程序启动时,我会检查可访问性,因为我需要即时的互联网连接。不过,我的问题是似乎没有立即确认 NetworkStatus,这意味着在设置可达性之后,我立即检查是否有连接,但它返回没有连接,无论如何我实际上是在使用 WiFi/3G,还是已关闭 radio 。

我可以确认我确实获得了 Internet 连接,因为在 applicationDidFinishLaunching 之后立即有一个通知,然后记录“ReachableViaWiFi”..

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

- (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;
}
}
}

最佳答案

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

-(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];
}

关于iphone - 在 Reachability (2.0.3ddg) 方面需要一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2156375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com