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

发布于 2024-12-23 14:12:36 字数 1487 浏览 5 评论 0原文

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

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

什么更好?

这是我迄今为止在应用程序委托中所做的事情:

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

    }
}

My iPhone app requires internet to function.

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?

What is better?

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光暖心i 2024-12-30 14:12:36

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

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

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

锁定 UI,并且在等待足够长的时间后不给用户任何选择可能会导致拒绝,无论您是否首先检查了可达性。

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 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.

慕巷 2024-12-30 14:12:36

这实际上取决于您的应用程序。您可以执行以下两项操作:在互联网不可用时禁用功能并通知用户。

我更喜欢在没有互联网连接时更改 UI,在启动任何需要连接的方法之前确保有连接。

It really depends on your application. You could do both: disable functionality when the internet is not available and inform the user.

I prefer to change the UI when there is no internet connection, and make sure there is connectivity before firing off any methods that require a connection.

素食主义者 2024-12-30 14:12:36

我建议:

1)在发出任何互联网请求之前始终检查互联网连接并进行适当处理。
2)有一个轮询机制来检查可达性。如果无法访问,可能会警告用户并禁用程序中发出互联网请求的部分。也许称之为“离线模式”。

I recommend:

1) Always check for internet connectivity prior to making any internet request and handle appropriately.
2) Have a polling mechanism that will check reachability. If unreachable, perhaps warn the user AND disable portions of your program that make internet requests. Maybe call it 'Offline Mode'.

罗罗贝儿 2024-12-30 14:12:36

从技术上讲,检查互联网是由开发者决定的,但苹果过去曾拒绝过应用程序,因为它们没有通知用户互联网连接不可用。

http://mobileorchard.com/avoiding-iphone-app-rejection-from-苹果/ (#6)

Checking for internet is technically up to you as the developer to decide, but Apple has in the past rejected apps for not notifying the users that the internet connection was not available.

http://mobileorchard.com/avoiding-iphone-app-rejection-from-apple/ (#6)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文