gpt4 book ai didi

ios - IOS 中的可达性和状态

转载 作者:行者123 更新时间:2023-11-28 22:20:38 24 4
gpt4 key购买 nike

我的 viewController.m 类中有以下代码:

     - (void) testInternetConnection
{
internetConnection = [Reachability reachabilityWithHostname:@"www.google.com"];

// Internet is reachable
internetConnection.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
});
};

// Internet is not reachable
internetConnection.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
});
};

[internetConnection startNotifier];
}
  1. 如何使用 startNotifier?
  2. 我是否必须将它放在每个我想测试互联网连接的 View Controller 中?

我用它来测试状态:

BOOL status = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);

最佳答案

startNotifier 意味着之后任何网络状态改变都会通知注册 kReachabilityChangedNotification 通知的人。

你不必把它放在每个 View Controller 中。

1、你需要一个单例实例,并有一个用于保持网络状态的成员值。

2、注册 kReachabilityChangedNotification 通知,处理它并获取网络状态并将其存储在您的成员值和 Post Notification(自定义通知)中以通知其他人(您的 View Controller )。

3、提供获取当前网络状态的接口(interface),以便当网络状态发生变化时,您的 View Controller 将知道网络状态。

关于ios - IOS 中的可达性和状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20513640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com