iPhone可达性检查

发布于 2024-08-14 06:08:21 字数 1721 浏览 8 评论 0原文

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

我的项目中有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)并不是主要功能。其他都不需要互联网访问。

我们没有添加更多代码,而是将两个互联网视图的背景设置为通知,告诉用户必须连接到互联网才能使用此功能。它与应用程序界面的其余部分一致,并且做得很好/很有品味。他们在批准过程中对此只字未提,但我们确实接到了一个私人电话,以确认我们正在赠送与电影实际相关的物品。根据他们通常含糊的协议,否则不允许您进行抽奖活动。

我还认为这也更严格地遵循他们的“仅在绝对需要时才使用东西”的理念。

这是指向应用程序 EvoScanner 的 iTunes 链接。

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.reachability problems
(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.

Here's the iTunes link to the application, EvoScanner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜未央樱花落 2024-08-21 06:08:21

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

https://developer.apple .com/library/content/samplecode/Reachability/Introduction/Intro.html

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

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

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

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:

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

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

in .m file:

[[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"); }
}
裂开嘴轻声笑有多痛 2024-08-21 06:08:21
[reachability setHostName:@"http://www.google.com"];

注意力!我遇到的问题是,如果使用 http:// 前缀,它总是“NotReachable”。

拉斐尔

[reachability setHostName:@"http://www.google.com"];

Attention! I encountered the problem that it's always "NotReachable" if the http:// prefix is used.

Raphael

じее 2024-08-21 06:08:21

这是正确的代码,因为它今天对我有用!

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

Here's the right code as it works for me today!!!

[[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"); }
念三年u 2024-08-21 06:08:21

您在任何地方都有以下代码吗?

[reachability startNotifier];

如果您的可达性代码来自苹果的示例,那么您需要在它开始向您报告状态更新之前执行此操作。

Do you have the following code anywhere?

[reachability startNotifier];

if your reachability code is from apple's example, then you need to do that before it can start reporting status updates to you.

最冷一天 2024-08-21 06:08:21

把这个改成

reachability = [Reachability reachabilityForInternetConnection];

这个

reachability = [[Reachability reachabilityForInternetConnection] retain];

change this

reachability = [Reachability reachabilityForInternetConnection];

to this

reachability = [[Reachability reachabilityForInternetConnection] retain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文