gpt4 book ai didi

ios - 如何在 iOS 中连接或断开 wifi 时收到通知?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:27 24 4
gpt4 key购买 nike

我正在尝试让 iOS 在建立或断开 wifi 连接时通知我的应用。

我在装有 iOS 4.3.5 和 5.1 的 iPhone 和 iPad 上尝试了以下代码:

- (void)startWifiNotifier {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusUpdated:)
name:kReachabilityChangedNotification
object:nil];

Reachability *wifi = [Reachability reachabilityForLocalWiFi];
[wifi startNotifier];
NSLog(@"Wifi status: %i", [wifi currentReachabilityStatus]);
NSLog(@"Notifier started");
}

- (void)statusUpdated:(NSNotification *)notice {
NetworkStatus s = [[notice object] currentReachabilityStatus];
if(s == NotReachable) {
NSLog(@"Wifi disconnected");
} else {
NSLog(@"Wifi connection established");
}
}

问题是我的回调“statusUpdated”永远不会被调用。我知道这应该按原样工作,但我尝试进入和离开 wifi 范围(因此连接和断开连接)并打开和关闭 wifi 路由器以强制断开连接,我的回调从未被调用。

我也试过这个:

@private
Reachability *wifi;
...

- (void) startTimerLog {
wifi = [Reachability reachabilityForLocalWiFi];
[self timerLog];
}

- (void) timerLog {
NSLog(@"Reachability: %i", [wifi currentReachabilityStatus]);
[self performSelector:@selector(timerLog) withObject:nil afterDelay:0.5];
}

并在相同的场景下对其进行了测试,它运行良好。然而,这不是我需要的方法,我更需要通知来调用我的回调。

我还需要做些什么才能使通知程序正常工作吗?

最佳答案

使用这个库 http://huytd.github.io/datatify/它非常易于使用和定制。setCallback 方法将帮助您检测连接何时更改

关于ios - 如何在 iOS 中连接或断开 wifi 时收到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12629671/

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