我什么时候应该检查互联网连接 [英] When should I check for internet connectivity

查看:179
本文介绍了我什么时候应该检查互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My iPhone app requires internet to function.

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

Should I checks for connectivity everytime a internet function is executed? Or should I use my appDelegate to listen for messages from something like the reachability class to determine if the connection was lost and warn the user?

什么是更好的?

这是我到目前为止在我的应用代理中所做的事情:

Here's what I've done so far in my app 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技术讲座主持人建议在大多数情况下不检查可达性,只是解除异步网络请求,通知用户(通过某种活动指示)应用正在等待网络如果他们认为他们已经等待了足够长的时间,而不是锁定用户界面或应用程序,则给予用户一个选项。

At least one Apple tech talk presenter recommended not checking Reachability in most cases, just firing off your async network requests, informing the user (via some type of activity indicator) that the app is waiting for a network response, and giving the user an option if in their opinion they've waited long enough, instead of locking up the UI or the app.

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

The reason is that, on a moving mobile device, the network can and will often go up between the Reachability tests and any actual data requests. Thus the user will be misinformed. The net can also go down after informing the user that there is connectivity, which can even be worse.

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

Plus Reachability only tells you about the connectivity of the nearest/1st network hop, which may or may not provide connectivity to the rest of the internet or to your destination site. Common example might be all those misconfigured WIFI access points.

锁定用户界面,并且在等待足够长时间后没有给用户任何选项可能是拒绝的理由,是否您首先检查了可达性。

Locking up the UI, and not giving the user any option after waiting long enough is likely grounds for rejection, whether or not you've checked Reachability first.

这篇关于我什么时候应该检查互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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