OSX 的网络连接 NSNotification?

发布于 2024-09-02 19:19:05 字数 115 浏览 4 评论 0原文

我只需要在分配有效 IP 地址时发出通知即可。我尝试过通过 SCreachability 进行轮询,但这似乎效率低下。

有什么建议吗?这看起来应该很简单,但我已经努力了几个小时才能让任何东西发挥作用。

I simply need to have a notification post when a valid IP address is assigned. I have tried polling via SCReachability, but that seems to be inefficient.

Any suggestions? This seems like it should be simple, but I've been struggling for several hours to get anything working.

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

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

发布评论

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

评论(2

笑饮青盏花 2024-09-09 19:19:05

我知道这有点旧,但所选的答案并不理想。

SCreachability API 可用于了解特定主机何时可访问。如果您想知道的是何时分配有效的 IP 地址(而不是到目标 IP 的可行路由),那么您应该查看 SCDynamicStore* API。
这些功能允许您订阅系统的配置存储,以便在某些值发生更改时收到通知。
因此,要在任何接口上的 IPv4 地址发生更改时发出通知,您需要执行以下操作(为了可读性而删除了错误检查)。

  SCDynamicStoreRef dynStore;

  SCDynamicStoreContext context = {0, NULL, NULL, NULL, NULL};

  dynStore = SCDynamicStoreCreate(kCFAllocatorDefault,
                                  CFBundleGetIdentifier(CFBundleGetMainBundle()),
                                  scCallback,
                                  &context);

    const CFStringRef keys[3] = {
        CFSTR("State:/Network/Interface/.*/IPv4")
    };
    CFArrayRef watchedKeys = CFArrayCreate(kCFAllocatorDefault,
                                         (const void **)keys,
                                         1,
                                         &kCFTypeArrayCallBacks);
    if (!SCDynamicStoreSetNotificationKeys(dynStore,
                                         NULL,
                                         watchedKeys)) 
  {
       CFRelease(watchedKeys);
        fprintf(stderr, "SCDynamicStoreSetNotificationKeys() failed: %s", SCErrorString(SCError()));
        CFRelease(dynStore);
        dynStore = NULL;

        return -1;
    }
    CFRelease(watchedKeys);

    rlSrc = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, dynStore, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), rlSrc, kCFRunLoopDefaultMode);
    CFRelease(rlSrc);

您将需要实现 scCallback 函数,每当发生更改时都会调用该函数。此外,您还需要在某处调用 CFRunLoopRun 以使代码运行。

I know this is a bit old but the selected answer is not ideal.

The SCReachability API can be used to know when a particular host is reachable. If all you want to know is when a valid IP address is assigned (rather than a viable route to a target IP) then you should look at the SCDynamicStore* APIs instead.
These functions allow you to subscribe to the system's configuration store for notification when certain values change.
So, for notification when the IPv4 address changes on any interface you would do the following (error checking removed for readability)

  SCDynamicStoreRef dynStore;

  SCDynamicStoreContext context = {0, NULL, NULL, NULL, NULL};

  dynStore = SCDynamicStoreCreate(kCFAllocatorDefault,
                                  CFBundleGetIdentifier(CFBundleGetMainBundle()),
                                  scCallback,
                                  &context);

    const CFStringRef keys[3] = {
        CFSTR("State:/Network/Interface/.*/IPv4")
    };
    CFArrayRef watchedKeys = CFArrayCreate(kCFAllocatorDefault,
                                         (const void **)keys,
                                         1,
                                         &kCFTypeArrayCallBacks);
    if (!SCDynamicStoreSetNotificationKeys(dynStore,
                                         NULL,
                                         watchedKeys)) 
  {
       CFRelease(watchedKeys);
        fprintf(stderr, "SCDynamicStoreSetNotificationKeys() failed: %s", SCErrorString(SCError()));
        CFRelease(dynStore);
        dynStore = NULL;

        return -1;
    }
    CFRelease(watchedKeys);

    rlSrc = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, dynStore, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), rlSrc, kCFRunLoopDefaultMode);
    CFRelease(rlSrc);

You will need to imlement the scCallback function which will be called whenever there is a change. Also you'll need to call CFRunLoopRun somewhere to make the code go.

一个人的夜不怕黑 2024-09-09 19:19:05

您不必使用 SCNetworkReachability 进行轮询。您注册回调,系统在网络状态发生变化时调用回调。这并不是低效。

请参阅 Apple 的示例代码 Reachability,其中展示了如何集成将其集成到 Objective-C 应用程序中。该代码适用于 iPhone,但我认为 Reachability.{m,h} 应该适用于 OS X。

(请参阅 Apple 的示例代码 SimpleReach。已经有五年了,但应该可以工作。)

You don't have to poll with SCNetworkReachability. You register the callback, and the system calls the callback when the network state changes. It's not inefficient.

See Apple's sample code Reachability which shows how to integrate it into an Objective-C app. The code is for iPhone, but I think Reachability.{m,h} should work on OS X.

(See Apple's sample code SimpleReach. It's five years old, but should work.)

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