gpt4 book ai didi

iphone - 我应该什么时候检查互联网连接

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:57 25 4
gpt4 key购买 nike

我的 iPhone 应用程序需要互联网才能运行。

我应该在每次执行互联网功能时检查连接吗?或者我应该使用我的 appDelegate 来监听来自可达性类之类的消息以确定连接是否丢失并警告用户?

什么更好?

这是我到目前为止在我的应用委托(delegate)中所做的:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

reachability = [[Reachability reachabilityWithHostName:[NSString stringWithFormat:@"http://%@/iSql/" ,[[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]]] retain];

[reachability startNotifier];

return YES;
}

- (void)reachabilityChanged:(NSNotification *)note {

NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];

if (ns == NotReachable) {

if (![networkAlert isVisible]) {

if ([self networkAlert] == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to retrieve data from the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[self setNetworkAlert:alert];
[alert release];
}

[networkAlert show];
}
} else {

if ([self networkAlert] != nil) {
[networkAlert dismissWithClickedButtonIndex:0 animated:YES];
}

}
}

最佳答案

至少一位 Apple 技术演讲主持人建议在大多数情况下不要检查可达性,只需触发异步网络请求,通知用户(通过某种类型的事件指示器)应用程序正在等待网络响应,并提供如果用户认为他们已经等待了足够长的时间,而不是锁定 UI 或应用程序,则用户可以选择。

原因是,在移动的移动设备上,网络可以而且将经常在可达性测试和任何实际数据请求之间上升。因此,用户会被误导。网络也可能在通知用户存在连接后出现故障,这甚至可能更糟。

Plus Reachability 仅告诉您最近/第一个网络跃点的连接性,它可能会或可能不会提供与互联网其余部分或您的目标站点的连接。常见的例子可能是所有那些错误配置的 WIFI 接入点。

锁定 UI,并且在等待足够长的时间后不给用户任何选项可能是拒绝的理由,无论是否您首先检查了 Reachability。

关于iphone - 我应该什么时候检查互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8660284/

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