objective-c iPhone 可达性检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1861656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:36:03  来源:igfitidea点击:

iPhone reachability checking

iphoneobjective-capp-storeappstore-approvalreachability

提问by Sneakyness

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

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

#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.

在我试图使用可达性的 .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"); }

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)

设置。这是迄今为止我最不喜欢的编程部分。(来源:snekyness.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.

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

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.

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

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.

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

回答by Kendall Helmstetter Gelner

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

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

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

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

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

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

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:

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

in .h file:

在 .h 文件中:

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

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

in .m file:

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

回答by Raphael Schaad

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

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

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

Raphael

拉斐尔

回答by nikoz

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

回答by Kevlar

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.

如果您的可达性代码来自苹果的示例,那么您需要先这样做,然后它才能开始向您报告状态更新。

回答by mhrrt

change this

改变这个

reachability = [Reachability reachabilityForInternetConnection];

to this

对此

reachability = [[Reachability reachabilityForInternetConnection] retain];