|
| 1 | +// |
| 2 | +// RNReachability.m |
| 3 | +// RNTwilioClient |
| 4 | +// |
| 5 | +// Created by Enrique Viard on 7/21/21. |
| 6 | +// Copyright © 2021 No Good Software Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "RNNetworkMonitor.h" |
| 10 | +@import Foundation; |
| 11 | +@import Network; |
| 12 | + |
| 13 | +@interface RNNetworkMonitor() |
| 14 | + |
| 15 | +@property (nonatomic, strong) nw_path_monitor_t monitor; |
| 16 | +@property (nonatomic, strong) dispatch_queue_t monitorQueue; |
| 17 | +@property (nonatomic, strong) nw_path_t path; |
| 18 | +@property (nonatomic, strong) NSString *pathDescription; |
| 19 | + |
| 20 | +@end |
| 21 | + |
| 22 | +@implementation RNNetworkMonitor |
| 23 | + |
| 24 | +- (void)startNetworkMonitoring { |
| 25 | + dispatch_queue_attr_t attrs = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, DISPATCH_QUEUE_PRIORITY_DEFAULT); |
| 26 | + self.monitorQueue = dispatch_queue_create("com.ngs.network.monitor", attrs); |
| 27 | + |
| 28 | + self.monitor = nw_path_monitor_create(); |
| 29 | + nw_path_monitor_set_queue(self.monitor, self.monitorQueue); |
| 30 | + nw_path_monitor_set_update_handler(self.monitor, ^(nw_path_t _Nonnull path) { |
| 31 | + self.path = path; |
| 32 | + nw_path_status_t status = nw_path_get_status(path); |
| 33 | + BOOL isWiFi = nw_path_uses_interface_type(path, nw_interface_type_wifi); |
| 34 | + BOOL isCellular = nw_path_uses_interface_type(path, nw_interface_type_cellular); |
| 35 | + self.pathDescription = isWiFi ? @"WIFI" : @"CELLULAR"; |
| 36 | + |
| 37 | + NSDictionary *userInfo = @{ |
| 38 | + @"path" : self.pathDescription, |
| 39 | + @"status" : @(status), |
| 40 | + }; |
| 41 | + |
| 42 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 43 | + NSLog(@"[IIMobile - RNNetworkMonitor] onNetworkStatusChange %@", userInfo); |
| 44 | + [NSNotificationCenter.defaultCenter postNotificationName:@"onNetworkStatusChange" object:nil userInfo:userInfo]; |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + nw_path_monitor_start(self.monitor); |
| 49 | +} |
| 50 | + |
| 51 | +- (void)stopNetworkMonitoring { |
| 52 | + nw_path_monitor_cancel(self.monitor); |
| 53 | +} |
| 54 | + |
| 55 | +- (NSString*)getCurrentPath { |
| 56 | + return self.pathDescription; |
| 57 | +} |
| 58 | + |
| 59 | ++ (NSString *)moduleName { |
| 60 | + return @"RNNetworkMonitor"; |
| 61 | +} |
| 62 | + |
| 63 | +@end |
| 64 | + |
0 commit comments