检测以太网/WiFi网络变化 [英] Detect ethernet/wifi network change

查看:99
本文介绍了检测以太网/WiFi网络变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测网络何时从以太网更改为wifi(或从wifi更改为以太网).我想请一位观察者通知我有关此更改的信息.

可到达性还不够好-两种情况下始终会返回ReachableViaWiFi.

P-S- 以前有一些关于此主题的问题,但是没有一个答案很好,而且由于这些问题已有一年多的历史了,因此也许有人已经找到了解决方法

解决方案

您可以通过SystemConfiguration模块访问系统网络偏好设置,该模块可以帮助您了解当前位于默认位置/Library/Preferences/SystemConfiguration/preferences.plist的系统偏好设置. >

从那时起,您可以通过SCDynamicStoreNotifyValue(_:_:)SCDynamicStore接收通知,或者通过SCDynamicStoreCopyValue(_:_:)检索值.

直接查找当前主网络服务的示例:

var store = SCDynamicStoreCreate(nil, "Example" as CFString, nil, nil)
var global = SCDynamicStoreCopyValue(store, "State:/Network/Global/IPv4" as CFString)!

var pref = SCPreferencesCreate(nil, "Example" as CFString, nil)
var service = SCNetworkServiceCopy(pref!, global["PrimaryService"] as! CFString)
var interface = SCNetworkServiceGetInterface(service!)

SCNetworkInterfaceGetInterfaceType(interface!) /// Optional("IEEE80211") -> Wi-Fi

或使用回调创建动态存储并设置通知键以在每次主要网络服务更改时接收通知:

var callback: SCDynamicStoreCallBack = { (store, _, _) in
  /* Do anything you want */
}
var store = SCDynamicStoreCreate(nil, "Example" as CFString, callback, nil)
SCDynamicStoreSetNotificationKeys(store!, ["State:/Network/Global/IPv4"] as CFArray, nil)

I want to detect when the network changes from ethernet to wifi (or wifi to ethernet). I want to have an observer to notify me about this change.

reachability isn't good enough - it's always returns ReachableViaWiFi for both cases.

P.S - There were some questions regarding this topic before, but none of them has a good answer, and since those questions are more than a year old, maybe someone already find out how to do it

解决方案

You can access system network preferences through SystemConfiguration module, which helps you get touch to system preferences store currently resides in the default location /Library/Preferences/SystemConfiguration/preferences.plist.

Since then, you can receive notifications from SCDynamicStore by SCDynamicStoreNotifyValue(_:_:) or retrieve value by SCDynamicStoreCopyValue(_:_:).

Example for directly lookup current primary network service:

var store = SCDynamicStoreCreate(nil, "Example" as CFString, nil, nil)
var global = SCDynamicStoreCopyValue(store, "State:/Network/Global/IPv4" as CFString)!

var pref = SCPreferencesCreate(nil, "Example" as CFString, nil)
var service = SCNetworkServiceCopy(pref!, global["PrimaryService"] as! CFString)
var interface = SCNetworkServiceGetInterface(service!)

SCNetworkInterfaceGetInterfaceType(interface!) /// Optional("IEEE80211") -> Wi-Fi

Or create dynamic store with callback and set notification keys to receive notifications as every time primary network service changes the notification is going to fire:

var callback: SCDynamicStoreCallBack = { (store, _, _) in
  /* Do anything you want */
}
var store = SCDynamicStoreCreate(nil, "Example" as CFString, callback, nil)
SCDynamicStoreSetNotificationKeys(store!, ["State:/Network/Global/IPv4"] as CFArray, nil)

这篇关于检测以太网/WiFi网络变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆