iPhone可达性检查
我找到了几个代码示例来执行我想要的操作(检查可达性),但似乎没有一个足够准确,对我有用。我不明白为什么这不想玩得好。
我的项目中有reachability.h/m,我正在做
#import <SystemConfiguration/SystemConfiguration.h>
并且我添加了框架。我还有:
#import "Reachability.h"
在 .m 的顶部,我试图在其中使用可达性。
Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"http://www.google.com"]; // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFiNetwork) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) {NSLog(@"cell"); }
这给我带来了各种各样的问题。我做错了什么?我是一个不错的编码员,只是当需要弄清楚需要将什么放在哪里才能实现我想做的事情时,我很难,无论我是否想知道我想做什么。 (太令人沮丧了)
更新:这就是发生的事情。这是在我的视图控制器中,我已经使用它
#import <SystemConfiguration/SystemConfiguration.h>
并
#import "Reachability.h"
进行了设置。这是迄今为止我最不喜欢的编程部分。
(来源:sneakyness.com)
FWIW,我们从未最终实现这个在我们的代码中。需要访问互联网的两个功能(参加抽奖和购买 DVD)并不是主要功能。其他都不需要互联网访问。
我们没有添加更多代码,而是将两个互联网视图的背景设置为通知,告诉用户必须连接到互联网才能使用此功能。它与应用程序界面的其余部分一致,并且做得很好/很有品味。他们在批准过程中对此只字未提,但我们确实接到了一个私人电话,以确认我们正在赠送与电影实际相关的物品。根据他们通常含糊的协议,否则不允许您进行抽奖活动。
我还认为这也更严格地遵循他们的“仅在绝对需要时才使用东西”的理念。
I've found several examples of code to do what I want (check for reachability), but none of it seems to be exact enough to be of use to me. I can't figure out why this doesn't want to play nice.
I have the reachability.h/m in my project, I'm doing
#import <SystemConfiguration/SystemConfiguration.h>
And I have the framework added. I also have:
#import "Reachability.h"
at the top of the .m in which I'm trying to use the reachability.
Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"http://www.google.com"]; // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFiNetwork) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) {NSLog(@"cell"); }
This is giving me all sorts of problems. What am I doing wrong? I'm an alright coder, I just have a hard time when it comes time to figure out what needs to be put where to enable what I want to do, regardless if I want to know what I want to do or not. (So frustrating)
Update: This is what's going on. This is in my viewcontroller, which I have the
#import <SystemConfiguration/SystemConfiguration.h>
and
#import "Reachability.h"
set up with. This is my least favorite part of programming by far.
(source: sneakyness.com)
FWIW, we never ended up implementing this in our code. The two features that required internet access (entering the sweepstakes, and buying the dvd), were not main features. Nothing else required internet access.
Instead of adding more code, we just set the background of both internet views to a notice telling the users they must be connected to the internet to use this feature. It was in theme with the rest of the application's interface, and was done well/tastefully. They said nothing about it during the approval process, however we did get a personal phone call to verify that we were giving away items that actually pertained to the movie. According to their usually vague agreement, you aren't allowed to have sweepstakes otherwise.
I would also think this adheres more strictly to their "only use things if you absolutely need them" ideaology as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从您的屏幕截图来看,您似乎没有将 Reachability 添加到您的项目中。您必须从 Apple 下载 Reachability:
https://developer.apple .com/library/content/samplecode/Reachability/Introduction/Intro.html
并将 .h 和 .m 文件添加到您的项目中。
更新:您注意到您具有可达性。但是看看最新的版本,我可以明白为什么你会出现列出的错误 - 他们更改了 API,并且你可能正在使用在其他地方找到的示例代码。尝试:
在 .h 文件中:
在 .m 文件中:
From your screen shot, it seems like you do not have Reachability added to your project. You must download Reachability from Apple:
https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html
And add both .h and .m files to your project.
Update: You noted you have Reachability. But looking at the most recent version, I can see why you have the errors you listed - they changed the API and you are probably using sample code you found somewhere else. Try:
in .h file:
in .m file:
注意力!我遇到的问题是,如果使用 http:// 前缀,它总是“NotReachable”。
拉斐尔
Attention! I encountered the problem that it's always "NotReachable" if the http:// prefix is used.
Raphael
这是正确的代码,因为它今天对我有用!
Here's the right code as it works for me today!!!
您在任何地方都有以下代码吗?
如果您的可达性代码来自苹果的示例,那么您需要在它开始向您报告状态更新之前执行此操作。
Do you have the following code anywhere?
if your reachability code is from apple's example, then you need to do that before it can start reporting status updates to you.
把这个改成
这个
change this
to this