使用Reachability有什么好处? [英] What is the advantage of using Reachability?

查看:115
本文介绍了使用Reachability有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Reachability比下面的代码有什么好处?我觉得Reachability拥有大量代码,但如果它以任何方式更好,那么我会使用它。

What is the advantage of the using Reachability over the code below? I feel that Reachability has a huge amount of code, but if it's better in any way, then I'd use that instead.

NSString *connectionString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

if ([connectionString length] == 0) {

    //No connection

}

现在已经批准,如果谷歌曾经失败,那么这将无效。但实际上没有机会发生这种情况。你怎么看?谢谢!

Now granted, if Google ever went down then this wouldn't work. But there's literally no chance of that happening. What do you think? Thanks!

推荐答案

这实际上是一个非常好的问题 - 非常好,我实际上在iOS开发人员访谈中使用它时我的公司是招聘:

This is actually quite a good question - so good that I actually use it in iOS developer interviews when my company is recruiting:


为什么Apple的可达性示例有数百行,当你可以使用一行检查URL是否有响应?

Why is Apple's reachability example hundreds of lines long, when you can just use a single line to check if a URL is responsive?

首先,网络可达性实际上是非常非常复杂。它不仅仅是测试URL。考虑以下示例:

Firstly, network reachability is actually very, very complicated. It's much more than simply testing for a URL. Think about the following examples:


  • 用户使用的是3G,但已经用尽了他们的数据限额,因此每
    请求重定向到运营商的网站。

  • The user is on 3G, but has used up their data allowance, so every request redirects to the carrier's site.

用户连接到需要
身份验证/登录的公共WiFi网络,因此请求重定向到登录页面

The user is connected to a public WiFi network that requires authentication / login, so the request redirects to a log in page

最后一个例子是非常常见 - 它一直在发生。但如果您使用 initWithContentsOfURL 您的应用程序会想象您有连接,而实际上您没有:您只需返回网络重定向的页面内容来自。

The last example is incredibly common - it happens all the time. But if you used initWithContentsOfURL your app would imagine you had connectivity, when in fact you didn't: you would simply have been returned the contents of the page the network had redirected you to.

这就是为什么Apple的代码比你最初想象的要复杂得多的原因之一。你不应该只是问我能否达到这个URL,但是从这个URL返回的数据,我希望它是

This is one reason why Apple's code is more complex than you might at first think it needs to be. You shouldn't just be asking "can I reach this URL", but "is the data being returned from this URL what I expect it to be".

但这真的只是冰山一角。除此之外,可达性还有很多 - 例如,我可能有一个需要下载大量信息的应用程序,比如50MB。如果用户在未经他们同意的情况下使用3G连接,那么简单地下载50MB数据将是一个坏主意 - 特别是如果他们正在漫游,或者是在受限制的数据计划上。所以Reachability还会告诉你用户所处的连接类型:EDGE,3G,WiFi等(*注意:请参阅下面的注释,这可能不是最好的建议)。

But that's really just the tip of the iceberg. Reachability does a lot more besides that - for example, I might have an app that needs to download a lot of information, say 50MB worth. It would be a bad idea to simply download 50MB of data if the user was on a 3G connection without their consent - especially if they are roaming, or on a restricted data plan. So Reachability will also tell you what type of connection the user is on: EDGE, 3G, WiFi, etc (*NB: see the note below, this is probably not the best advice).

Reachability中的 ReadMe.txt 将告诉您更多关于代码可以做什么和不能做什么的内容。

The ReadMe.txt inside Reachability will tell you a little more about what the code can and can't do.

不幸的是,网上有太多人没有意识到有很多日常场景 initWithContentsOfURL 将返回有效的响应,但用户将不会具有连接性。 [博客这样的帖子] [1]在Google中被编入索引,人们认为它是可接受的替代品:它不是!

Unfortunately, there are all too many people online who don't realise that there are many everyday scenarios where initWithContentsOfURL will return a valid response but the user won't have connectivity. [Blog posts like this][1] get indexed in Google, and people assume it's an acceptable substitute: it's not!

我在招聘时提出这个问题的原因之一就是它可以向开发人员展示不仅仅是在思考问题 - 就像你和许多其他开发人员一样,我的第一反应是我看到Reachability示例代码哇,这看起来太复杂了,非常简单。但希望这个答案会在某种程度上说服你。

One of the reasons I ask this question when recruiting is it can show a developer is not just thinking inside the box - like you and many other developers, my first reaction when I saw the Reachability sample code was "wow, this seems way too complicated for something that's very simple". But hopefully this answer will have gone some way to convincing you otherwise.

编辑:请注意以下Steven的评论。他提出了我的答案没有考虑的一些要点(即MiFi热点),并提出了一个有效的案例,即Reachability不一定是代码编码天堂的顶峰。在许多情况下,开发人员会使用自己的改进等修改Reachability。

Definitely take note of Steven's comments below. He raises some points that my answer hadn't considered (i.e., MiFi hotspots), and makes a valid case that Reachability isn't necessarily the peak of coding heaven it code be. In many cases developers will modify Reachability with their own improvements and the like.

这篇关于使用Reachability有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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