iPhone 可达性检查 [英] iPhone reachability checking

查看:28
本文介绍了iPhone 可达性检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了几个代码示例来执行我​​想要的操作(检查可达性),但似乎没有一个对我有用.我不明白为什么这不想玩得开心.

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.

我的项目中有reachability.h/m,我正在做

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"

在我尝试使用可达性的 .m 的顶部.

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>

#import "Reachability.h"

设置.这是迄今为止我最不喜欢的编程部分.
(来源:sneakyness.com)

set up with. This is my least favorite part of programming by far.
(source: sneakyness.com)

FWIW,我们从未最终在我们的代码中实现这一点.需要互联网访问的两个功能(参加抽奖活动和购买 DVD)并不是主要功能.没有其他需要互联网访问.

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.

我们没有添加更多代码,而是将两个 Internet 视图的背景设置为通知,告诉用户他们必须连接到 Internet 才能使用此功能.它与应用程序界面的其余部分是主题,并且做得很好/很有品味.他们在审批过程中什么也没说,但是我们确实接到了一个私人电话,以确认我们赠送的物品实际上与电影有关.根据他们通常含糊不清的协议,否则您不得参加抽奖活动.

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.

这是应用程序 EvoScanner 的 iTunes 链接.

推荐答案

从您的屏幕截图来看,您似乎没有将 Reachability 添加到您的项目中.您必须从 Apple 下载 Reachability:

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

并将 .h 和 .m 文件添加到您的项目中.

And add both .h and .m files to your project.

更新:你注意到你有可达性.但是查看最新版本,我可以理解为什么您会出现您列出的错误 - 他们更改了 API,您可能正在使用在其他地方找到的示例代码.试试:

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:

在 .h 文件中:

//import Reachability class
#import "Reachability.h"

// declare Reachability, you no longer have a singleton but manage instances
Reachability* reachability;

在 .m 文件中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

 if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); }

.....

- (void) handleNetworkChange:(NSNotification *)notice
{

  NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

   if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); }
}

这篇关于iPhone 可达性检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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