gpt4 book ai didi

objective-c - wifi 连接时的通知 (OS X)

转载 作者:行者123 更新时间:2023-12-02 22:37:31 26 4
gpt4 key购买 nike

当机场连接到 ap 时,我需要来自系统的通知。有没有可能用 SystemConfiguration 框架做到这一点?我在理解 systemconfigurations api 文档时遇到问题。

最佳答案

SystemConfiguration 网络让您走在正确的轨道上,它提供了 SCNetworkReachability功能集。你可以尝试使用

SCNetworkReachabilitySetCallback

设置一个回调函数,当可达性改变时调用该回调函数;和

SCNetworkReachabilityScheduleWithRunLoop

在运行循环中安排可达性检查

或者您可以尝试使用 reachability framework (适用于 MacOS 和 iOS)它建立在 SystemConfiguration 框架之上,使事情变得更容易(更高级别)。

如果您想采用 SystemConfiguration 方式,这就是您检查当前可达性并安装回调以接收更改通知的方式 (source):

- (void)checkReachability {
NSString *server = [[NSUserDefaults standardUserDefaults] stringForKey:@"NCIDServer"];

if (server == nil) {
ncid_message_callback(self, [NSLocalizedString(@"No caller ID server was specified.", nil) UTF8String]);
return;
}

const char *serverName = [[[server componentsSeparatedByString:@":"] objectAtIndex:0] UTF8String];
SCNetworkReachabilityContext context = {0, (void *)self, NULL, NULL, NULL};
networkReachability = SCNetworkReachabilityCreateWithName(NULL, serverName);

if (networkReachability == NULL)
goto fail;

// If reachability information is available now, we don't get a callback later
SCNetworkConnectionFlags flags;

if (SCNetworkReachabilityGetFlags(networkReachability, &flags))
networkReachabilityCallback(networkReachability, flags, self);

if (!SCNetworkReachabilitySetCallback(networkReachability, networkReachabilityCallback, &context))
goto fail;

if (!SCNetworkReachabilityScheduleWithRunLoop(networkReachability, [[NSRunLoop currentRunLoop] getCFRunLoop], kCFRunLoopCommonModes))
goto fail;
return;

fail:
if (networkReachability != NULL)
CFRelease(networkReachability);

networkReachability = NULL; //-- ivar representing current reachability

}

这是回调的示例:

static void networkReachabilityCallback(SCNetworkReachabilityRef target,
SCNetworkConnectionFlags flags,
void *object) {
// Observed flags:
// - nearly gone: kSCNetworkFlagsReachable alone (ignored)
// - gone: kSCNetworkFlagsTransientConnection | kSCNetworkFlagsReachable | kSCNetworkFlagsConnectionRequired
// - connected: kSCNetworkFlagsIsDirect | kSCNetworkFlagsReachable

if (networkReachability == NULL)
return;

if ((flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired)) {

if (isReachable) // typically receive a reachable message ~20ms before the unreachable one
return;

isReachable = YES;
ncid_network_kill();
[NSThread detachNewThreadSelector:@selector(runThread:) toTarget:object withObject:nil];

} else {
isReachable = NO;
ncid_network_kill();
}
}

关于objective-c - wifi 连接时的通知 (OS X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11240196/

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