iPhone可达性检查
-
13-09-2019 - |
题
我发现的代码的几个例子做我想做的(检查可达性)的东西,但没有它似乎是足够准确的是使用的给我。我想不通为什么不想玩好。
我在我的项目中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"); }
这是给我各种各样的问题。我究竟做错了什么?我是一个正常的编码器,我只是有一个困难时期,当谈到时间来找出哪些需要放在哪里,使我想做的事,不管我想知道我想要做还是不做。 (因此,令人沮丧)
更新:这是怎么回事。这是在我的ViewController,我有
#import <SystemConfiguration/SystemConfiguration.h>
和
#import "Reachability.h"
设置了。这是迄今为止节目我最不喜欢的 结果 <子>(来源: sneakyness.com )子>
FWIW,我们从未结束了在我们的代码实现这一点。这两个功能需要网络连接(进入抽奖和买DVD),不是主要特点。没有别的要求互联网接入。
而不是增加更多的代码,我们刚才设置的两个互联网意见的通知,告诉他们必须连接到互联网才能使用该功能的用户的背景。这是在应用程序界面的其余部分的主题,并且做得很好/高雅。他们说,在审批过程中它什么,但是我们没有获得个人电话,以确认我们正在泄露,实际上关于有电影项目。据他们通常含混不清的协议,你不允许有抽奖活动,否则。
我也觉得这个坚持更加严格地以自己的ideaology以及“只有当你确实需要他们用的东西”。
解决方案
从你的屏幕截图,好像你没有可达添加到项目中。你必须从苹果下载可达:
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"); }
}
其他提示
[reachability setHostName:@"http://www.google.com"];
注意!我遇到它始终“NotReachable”如果HTTP的问题:使用//前缀
圣拉斐尔
下面是正确的代码,因为它今天对我的作品!
[[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"); }
你有下面的代码的任何地方?
[reachability startNotifier];
如果您的可达性的代码是从苹果公司的例子,那么你需要做的,然后才能开始报告状态更新到你。
更改此
reachability = [Reachability reachabilityForInternetConnection];
此
reachability = [[Reachability reachabilityForInternetConnection] retain];