最佳实践 - 无网络连接
您认为处理无网络连接的最佳方法是什么?
我未来的应用程序严重依赖网络,没有网络就没有任何事情可做。
我看到应用程序以不同的方式处理这种情况。
例如,当我没有连接时,eBay 应用程序有时会提取最后保存的数据,但有时它会清除所有内容。 Paypal 应用程序要么将我注销,要么就会崩溃。其中一个新闻应用程序会一直说正在加载...
有没有最佳实践?
应用程序应该继续尝试并显示加载消息、添加刷新按钮还是自动关闭应用程序?
谢谢
What do you think would be the best way to handle no network connectivity?
My future app heavily relies on network and without it there will be nothing to do with it.
I see apps handling the situation in different ways.
When I don't have connection the ebay app for example sometimes pulls the last saved data, but some other time it just clears everything out. Paypal app will either log me out or it will just crash. One of the news app will just keep say loading...
Is there any best practicies out there?
Should the app keep trying and display loading message, add a refresh button or just close the app automatically?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“无网络”情况的正确处理方式完全取决于您的应用程序。
需要记住两件事:以编程方式关闭应用程序或未能顺利处理“无连接可用”情况都是苹果审核过程中拒绝的原因。
拥有设计严重依赖网络可用的应用程序的经验,我想说最佳实践可以总结如下:
1)使用 Reachability 框架来检测您拥有哪种类型的连接;我链接到的版本优先于Apple提供的版本,后者存在缺陷(搜索SO以获取更多相关信息);
2)每次需要从网络获取数据时,检查连接是否恢复(即,不仅仅是开始一次);
3)当发生这种情况时,始终通过显示警报或更好地显示某种其他类型的图标/消息来通知用户没有可用的网络;干扰性最小的显示方式是最好的,这样就不会扰乱用户的工作流程;
4)如果您有缓存数据并且这是有意义的,请在没有可用网络连接时使用缓存数据(这也将使应用程序在这种情况下“部分可用”);
5) 为用户提供刷新数据的按钮(如果这对您的应用有意义);
也就是说,这种“最佳实践”的最简单实现是一个应用程序,在没有可用连接的情况下,让用户返回“主屏幕”(丑陋,但它可能是应用程序中唯一明智的事情)。完整的实现是,RSS 阅读器缓存所有新闻,并允许您在没有连接的情况下阅读它们(显然,无需加载更新的数据)。
What the proper handling of "no network" condition is, entirely depends on your application.
Two things to keep in mind: programmatically closing an app or not handling smoothly the "no connection available" case are both reasons for rejection in the Apple review process.
Having had experience with designing an app that also heavily depended on network being available, I would say that best practice can be summarized in the following:
1) use the Reachability framework to detect what kind of connectivity you have; the version I link to is to be preferred to the one provided by Apple, which is buggy (search S.O for more on this);
2) each time you need data from the network, check whether connection is back (i.e., not just once at the beginning);
3) always inform the user that no network is available, when this occurs, either by displaying an alert, or better showing some other kind of icon/messaging; the least intrusive kind of display is best, so to not disrupt the user's workflow;
4) if you have cached data and this makes sense, use cached data when no network connection is available (this will make the app "partially usable" in such cases as well);
5) offer the user a button to refresh the data, if this make sense for your app;
This said, the simplest implementation of this "best practice" is an app that in case no connection is available, gets the user back to the "main screen" (ugly, but it could be the only sensible thing in your app). Full implementation is, say, an rss reader caching all the news and allowing you to read them even when no connection is there (without loading newer data, obviously).
首先最简单的事情是:您永远不应该自己关闭应用程序,并且永远不应该崩溃。
如果显示旧数据适合您的情况,请这样做,并可能在屏幕上添加一些用户离线的通知。
如果没有在线访问,用户确实无能为力,请将所有内容灰显或使用类似的技术。
继续尝试在后台重新连接(即监听网络可用性变化)并采取适当的行为。
需要记住的一件事是:使用移动设备时,经常会出现短暂的断开连接,您需要妥善处理。
Easiest thing first: You should never ever close the app yourself, and you should never crash.
If displaying old data is appropriate in your case, just do so and maybe add some notice on the screen that the user is offline.
If there is really nothing the use can do without online access, gray everything out or use a similar technique.
Keep trying to reconnect in background (i.e. listen to network availability changes) and behave appropriately.
One thing to keep in mind: With mobile devices there are often short disconnects that you want to handle gracefully.
看看ASIHttpRequest,它有一个很棒的缓存机制。
Have a look at ASIHttpRequest, it has a great caching mechanism.
如果您的应用程序中没有网络连接,那么最好说“无网络连接”类型的消息,而不是显示加载/刷新等。
或者将数据存储在数据库中也是一个不错的选择,至少用户可以使用离线模式下的数据,您的应用程序也将在离线模式下使用。
(正如您提到的我未来的应用程序严重依赖网络,没有网络就没有任何关系。)。
确保您的应用程序在网络不可用时不会崩溃。
if there is no network connectivity in you application then it's better to say "No network connectivity" kind of message rather than showing loading/refreshing etc.
Or storing the data in database would also be a good option , At least user can play with the data in offline mode, and also your application will be used in offline mode as well.
(As you mentioned My future app heavily relies on network and without it there will be nothing to do with it. ).
Make sure, your application should not crash when the network is unavailable.