From 1a97e39d8ee3bbdc122b4c71feedc7e0ea23e822 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Tue, 25 Oct 2022 17:24:11 +0800 Subject: [PATCH] fix(ios): avoid CTTelephony's occasional crash --- ios/sdk/module/netinfo/HippyNetInfoIntenal.h | 28 ++++++++++-------- ios/sdk/module/netinfo/HippyNetInfoIntenal.m | 31 ++++++++++++++------ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/ios/sdk/module/netinfo/HippyNetInfoIntenal.h b/ios/sdk/module/netinfo/HippyNetInfoIntenal.h index 81120591f..10c292260 100644 --- a/ios/sdk/module/netinfo/HippyNetInfoIntenal.h +++ b/ios/sdk/module/netinfo/HippyNetInfoIntenal.h @@ -38,21 +38,16 @@ extern NSString *const HippyNetworkCellType5G; @interface HippyNetworkTypeObject : NSObject -/** - * indicate network type - * value:HippyNetworkTypeUnknown,HippyNetworkTypeNone,HippyNetworkTypeWifi,HippyNetworkTypeCell - */ +/// value: HippyNetworkTypeUnknown,HippyNetworkTypeNone,HippyNetworkTypeWifi,HippyNetworkTypeCell @property(nonatomic, readonly) NSString *networkType; -/** - * indicate cell type - */ +/// cell type @property(nonatomic, readonly) NSString *cellType; +/// init - (instancetype)initWithNetworkType:(NSString *)networkType cellType:(NSString *)cellType; -- (BOOL)isEqual:(id)object; - +/// isEqual's imp - (BOOL)isEqualToNetowrkTypeObject:(HippyNetworkTypeObject *)object; @end @@ -65,16 +60,23 @@ extern NSString *const HippyNetworkCellType5G; @interface HippyNetInfoIntenal : NSObject +/// Singleton + (instancetype)sharedInstance; +/// Get network type - (HippyNetworkTypeObject *)currentNetworkType; -/** - * set an observer for network changed event. - * return current network info immediately - */ +/// Get cell type +- (NSString *)currentCellType; + +/// Set an observer for network changed event. +/// and return current network info immediately +/// +/// - Parameter observer: observer - (HippyNetworkTypeObject *)addNetworkTypeChangeObserver:(id)observer; +/// Remove an observer for network changed event. +/// - Parameter observer: observer - (void)removeNetworkTypeChangeObserver:(id)observer; @end diff --git a/ios/sdk/module/netinfo/HippyNetInfoIntenal.m b/ios/sdk/module/netinfo/HippyNetInfoIntenal.m index cae6cd922..1da0b44d5 100644 --- a/ios/sdk/module/netinfo/HippyNetInfoIntenal.m +++ b/ios/sdk/module/netinfo/HippyNetInfoIntenal.m @@ -76,6 +76,9 @@ static HippyNetInfoIntenal *instance = nil; NSString *_host; CTTelephonyNetworkInfo *_cellNetType; NSHashTable *_observers; + + NSString *_cachedCellType; + BOOL _isRadioAccessTechnologyDirty; } @end @@ -87,8 +90,10 @@ static NSString *radioAccessNameIn(CTTelephonyNetworkInfo *networkInfo) { if (networkInfo.dataServiceIdentifier) { return [networkInfo.serviceCurrentRadioAccessTechnology objectForKey:networkInfo.dataServiceIdentifier]; } + return nil; + } else { + return networkInfo.currentRadioAccessTechnology; } - return networkInfo.currentRadioAccessTechnology; } static NSString *hippyReachabilityGetCellType(NSString *cellType) { @@ -153,7 +158,7 @@ static NSString *currentReachabilityType(SCNetworkReachabilityRef reachabilityRe static void reachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) { HippyNetInfoIntenal *netinfo = (__bridge HippyNetInfoIntenal *)info; NSString *networkType = hippyReachabilityTypeFromFlags(flags); - NSString *cellType = hippyReachabilityGetCellType(radioAccessNameIn(netinfo->_cellNetType)); + NSString *cellType = [netinfo currentCellType]; HippyNetworkTypeObject *obj = [[HippyNetworkTypeObject alloc] initWithNetworkType:networkType cellType:cellType]; [netinfo notifyObserversNetworkTypeChanged:obj]; } @@ -175,19 +180,19 @@ static void reachabilityCallback(__unused SCNetworkReachabilityRef target, SCNet _observers = [NSHashTable weakObjectsHashTable]; _reachability = createReachabilityRefWithZeroAddress(); _cellNetType = [[CTTelephonyNetworkInfo alloc] init]; + _isRadioAccessTechnologyDirty = YES; [self registerNetworkChangedListener]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioAccessTechnologyDidChange:) name:CTRadioAccessTechnologyDidChangeNotification object:nil]; } -- (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} - - (void)radioAccessTechnologyDidChange:(NSNotification *)notification { - HippyNetworkTypeObject *object = [self currentNetworkType]; - [self notifyObserversNetworkTypeChanged:object]; + _isRadioAccessTechnologyDirty = YES; + dispatch_async(dispatch_get_main_queue(), ^{ + HippyNetworkTypeObject *object = [self currentNetworkType]; + [self notifyObserversNetworkTypeChanged:object]; + }); } - (void)registerNetworkChangedListener { @@ -210,11 +215,19 @@ static void reachabilityCallback(__unused SCNetworkReachabilityRef target, SCNet - (HippyNetworkTypeObject *)currentNetworkType { NSString *networkType = currentReachabilityType(_reachability); - NSString *cellType = hippyReachabilityGetCellType(radioAccessNameIn(_cellNetType)); + NSString *cellType = [self currentCellType]; HippyNetworkTypeObject *obj = [[HippyNetworkTypeObject alloc] initWithNetworkType:networkType cellType:cellType]; return obj; } +- (NSString *)currentCellType { + if (_isRadioAccessTechnologyDirty) { + _cachedCellType = hippyReachabilityGetCellType(radioAccessNameIn(_cellNetType)); + _isRadioAccessTechnologyDirty = NO; + } + return _cachedCellType; +} + - (void)notifyObserversNetworkTypeChanged:(HippyNetworkTypeObject *)object { NSArray> *observers = [_observers allObjects]; for (id observer in observers) {