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

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

问题描述

我发现code的几个例子做我想做(检查可达性)的东西,但没有它似乎是不够确切的是利用我。我想不通为什么不想玩好。

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 /平方米,我做

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"); }

这是给我各种各样的问题。我究竟做错了什么?我是一个正常的codeR,我只是有一个困难时期,当谈到时间弄清楚哪些需要放在哪里,让我想做的事,不管我想知道我想要做还是不做。 (如此令人沮丧)

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)

更新:这是怎么回事。这是我的ViewController,我有

Update: This is what's going on. This is in my viewcontroller, which I have the

#import <SystemConfiguration/SystemConfiguration.h>

#import "Reachability.h"

设置了。这是节目我最不喜欢的部分远远

set up with. This is my least favorite part of programming by far.


FWIW,我们从未结束了在我们的code实现此。这两个功能需要网络连接(进入抽奖和购买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.

而不是增加更多的code,我们刚刚成立的两个互联网意见的通知,告诉他们必须连接到互联网才能使用该功能的用户的背景。它是在与应用程序界面的其余部分的主题,并且做得很好/别有风味。他们说,在审批过程中它没有什么,但是我们确实得到了个人的电话以验证我们放弃,实际上涉及到电影项目。据他们通常含糊的协议,你不允许有抽奖活动,否则。

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.

我也觉得这遵守更严格的自己ideaology以及只有当你绝对需要他们用的东西。

I would also think this adheres more strictly to their "only use things if you absolutely need them" ideaology as well.

下面是iTunes的链接到应用程序,EvoScanner。

推荐答案

从你的屏幕截图,好像你没有可达添加到您的项目。必须从苹果网站下载可达:

From your screen shot, it seems like you do not have Reachability added to your project. You must download Reachability from Apple:

<一个href=\"http://developer.apple.com/iphone/library/sample$c$c/Reachability/index.html\">http://developer.apple.com/iphone/library/sample$c$c/Reachability/index.html

和两者的.h和.m文件添加到项目中。

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

更新:你提到你的可达性。但看最新的版本,我可以看到,为什么你有你列出的错误 - 他们改变了API,你可能正在使用样本code你找到了别的地方。尝试:

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天全站免登陆