如何在 Objective-C 中验证网络连接

发布于 2024-10-17 16:22:02 字数 200 浏览 0 评论 0原文

我正在查看developer.apple.com 上的Reachability 示例项目,发现这是一个大型项目,只是为了验证您是否具有网络连接。

问题的第一部分是“确定设备是否可以连接 3G 或 WiFi 网络所需的最低代码是什么?”

接下来,这应该在 appDelegate 内部(启动时)还是在启动的第一个视图控制器内完成?

先感谢您

I was looking over the Reachability sample project on developer.apple.com and found that it was a large project just to verify you had network connectivity.

First part of the question is "What is the minimum code required to find out if the device can reach a 3G or wifi network?"

And next should this be done inside the appDelegate (on start) or inside the first View Controller that is launched?

Thank you in advance

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

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

发布评论

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

评论(1

つ可否回来 2024-10-24 16:22:02

它并不大,它确实可以满足您的要求。如果它对您来说太大,您可以只提取您需要的内容,例如reachabilityForLocalWiFi。但恐怕也不会小很多。

是的,您可以在应用程序委托中或第一个视图控制器内使用可达性。

Reachability 通知注册……

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(networkReachabilityDidChange:)
                                             name:kReachabilityChangedNotification
                                           object:nil];
__reachability = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[__reachability startNotifier];

回调方法示例……

- (void)networkReachabilityDidChange:(NSNotification *)notification {
  Reachability *reachability = ( Reachability * )[notification object];
  if ( reachability.currentReachabilityStatus != NotReachable ) {
    // Network is available, ie. www.google.com
  } else {
    // Network is not available, ie. www.google.com
  }
}

不要忘记停止通知、移除观察者并释放 rechability 对象。

It's not large, it really does what you want. If it is too big for you, you can extract what you need only like reachabilityForLocalWiFi. But I'm afraid that it will not be much smaller.

Yes, you can use reachability in your application delegate or inside the first view controller.

Reachability notification registration ...

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(networkReachabilityDidChange:)
                                             name:kReachabilityChangedNotification
                                           object:nil];
__reachability = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[__reachability startNotifier];

... callback method example ...

- (void)networkReachabilityDidChange:(NSNotification *)notification {
  Reachability *reachability = ( Reachability * )[notification object];
  if ( reachability.currentReachabilityStatus != NotReachable ) {
    // Network is available, ie. www.google.com
  } else {
    // Network is not available, ie. www.google.com
  }
}

... do not forget to stop notifications, remove observer and release rechability object.

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