我已经找到了一些代码的例子来做我想做的事情(判断可达性),但没有一个看起来足够精确,对我有用.我不明白为什么这不想玩得很好.

我有可达性.h/m在我的项目中,我正在做

#import <SystemConfiguration/SystemConfiguration.h>

我添加了框架.我还有:

#import "Reachability.h"

在最高层.我试图使用可达性.

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"

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, 和 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, 和 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.

我还认为这更严格地遵循了他们的"只有在你绝对需要的时候才使用"理念.

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

推荐答案

从您的屏幕截图来看,您的项目似乎没有添加可访问性.您必须从Apple下载可达性:

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

并将两者相加.h和.m文件到你的项目中.

更新:你注意到你有可达性.但是看看最新的版本,我可以理解为什么会出现列出的错误——它们更改了API,而且您可能正在使用在其他地方找到的示例代码.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"); }
}

Objective-c相关问答推荐

应用程序终止时处理推送通知

在 iOS 中检测 PAN 手势的方向

在 Objective C 中将属性标记为已弃用

将 NSInteger 变量传递给 NSMutableDictionary 或 NSMutableArray

pathForResource 返回 null

定义缓存变量时在objective-c中使用static关键字

带有 IB_DESIGNABLE 的 UIButton 会引发运行时属性警告,并且不会在 Interface Builder 中呈现

Objective-C 属性的默认属性是什么?

如何更改 UICollectionView 中的滚动方向?

Xcode:可以为协议接口所需的方法自动创建存根吗?

Objective-C:如何从子类访问父属性?

iOS:使用设备修饰符加载 xib 文件?

以编程方式创建 UITableView

Objective-C 的 NSMutableArray 是线程安全的吗?

两个 NSDate 对象之间的区别 - 结果也是一个 NSDate

将 UIColor 保存到 NSUserDefaults 并从中加载

以编程方式设置 UIView 的自动调整大小掩码?

ARC 下的 NSString 属性应该是强的还是复制的?

Xcode 警告未使用属性访问结果 - getter 不应用于副作用

Block 隐式保留'self';明确提及touch以表明这是预期行为