是否可以检测硬件更改?我正在寻找更改网络适配器时发送的更新通知(即当用户取消其MacBook时)。
我已经用Google搜索,但无法找到任何内容。
答案 0 :(得分:2)
我使用过#import <AFNetworking/AFNetworkReachabilityManager.h>
- (instancetype)init
{
self = [super init];
if( self )
{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:AFNetworkingReachabilityDidChangeNotification
object:nil];
}
return self;
}
-(void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AFNetworkingReachabilityDidChangeNotification
object:nil];
}
,发现它最容易使用(监听适配器更改)
- (void)reachabilityChanged:(NSNotification *)notification
{
NSNumber *s = notification.userInfo[AFNetworkingReachabilityNotificationStatusItem];
AFNetworkReachabilityStatus status = [s integerValue];
//Check value of status here
}
然后实现处理程序:
#import <SystemConfiguration/SystemConfiguration.h>
BOOL networkAvailable = NO;
const char *hostName = [NEVER_TRANSLATE(@"google.com") cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityFlags flags = 0;
SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithName(NULL, hostName);
if (SCNetworkReachabilityGetFlags(target, &flags))
{
if (flags == kSCNetworkFlagsReachable)
{
networkAvailable = YES;
}
}
这是标题,您可以在其中查看用于状态的常量: https://github.com/AFNetworking/AFNetworking/blob/4fb051898c60051d207ca0c24435766fdf140e34/AFNetworking/AFNetworkReachabilityManager.h
我还使用了SCReachability
Dim textToLookAt = txtPartNum.Text
If Not (System.Text.RegularExpressions.Regex.IsMatch(textToLookAt, "^[0-9]{2}[A-Za-z]{2}")) Then
MessageBox.Show("Error: Part number must contain two numbers followed by two letters. first")
blnValid = False
End If
虽然我用这个来检测特定时间的网络可用性,而不是注册为监听器,所以不确定这是否能达到你想要的效果