-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathNetworkInterfaceMonitor.m
60 lines (45 loc) · 1.5 KB
/
NetworkInterfaceMonitor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// NetworkInterfaceMonitor.m
// vmx
//
// Created by Boris Remizov on 07/10/16.
// Copyright © 2016 Veertu Labs Ltd. All rights reserved.
//
#import "NetworkInterfaceMonitor.h"
#import <SystemConfiguration/SystemConfiguration.h>
@interface NetworkInterfaceMonitor()
@property (nonatomic, strong) NSArray* interfaces;
@end
static void StoreCallBack(SCDynamicStoreRef store, CFArrayRef changedKeys, void* info) {
assert([NSThread isMainThread]);
NetworkInterfaceMonitor* me = (__bridge NetworkInterfaceMonitor*)info;
me.interfaces = CFBridgingRelease(SCNetworkInterfaceCopyAll());
}
@implementation NetworkInterfaceMonitor
{
SCDynamicStoreRef _store;
CFRunLoopSourceRef _source;
}
- (instancetype)init {
self = [super init];
if (!self)
return nil;
SCDynamicStoreContext context = {
0,
(__bridge void*)self,
CFRetain, CFRelease, CFCopyDescription
};
_store = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("NetworkInterfaceModitor"), StoreCallBack, &context);
NSArray *patterns = @[@"State:/Network/Interface"];
SCDynamicStoreSetNotificationKeys(_store, NULL, (__bridge CFArrayRef)patterns);
_source = SCDynamicStoreCreateRunLoopSource(NULL, _store, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), _source, kCFRunLoopCommonModes);
_interfaces = (__bridge NSArray*)SCNetworkInterfaceCopyAll();
return self;
}
- (void)dealloc {
CFRunLoopSourceInvalidate(_source);
CFRelease(_source);
CFRelease(_store);
}
@end