@@ -2,23 +2,11 @@ import Foundation
22import Network
33
44// MARK: - SentryConectivity
5- #if DEBUG || SENTRY_TEST || SENTRY_TEST_CI
6- @objc @_spi ( Private)
7- public enum SentryConnectivity : Int {
8- case cellular
9- case wiFi
10- case none
11- }
12- #else
13- @objc
145enum SentryConnectivity : Int {
156 case cellular
167 case wiFi
178 case none
18- }
19- #endif // DEBUG || SENTRY_TEST || SENTRY_TEST_CI
209
21- extension SentryConnectivity {
2210 func toString( ) -> String {
2311 switch self {
2412 case . cellular:
@@ -41,9 +29,9 @@ public protocol SentryReachabilityObserver: NSObjectProtocol {
4129public class SentryReachability : NSObject {
4230 private var reachabilityObservers = NSHashTable< SentryReachabilityObserver> . weakObjects( )
4331 private var currentConnectivity : SentryConnectivity = . none
44- private var pathMonitor : Any ? // NWPathMonitor for iOS 12+
32+ private var pathMonitor : NWPathMonitor ?
4533 private let reachabilityQueue : DispatchQueue = DispatchQueue ( label: " io.sentry.cocoa.connectivity " , qos: . background, attributes: [ ] )
46- private let observersLock = NSLock ( )
34+ private let observersLock = NSRecursiveLock ( )
4735
4836#if DEBUG || SENTRY_TEST || SENTRY_TEST_CI
4937 @objc public var skipRegisteringActualCallbacks = false
@@ -77,24 +65,10 @@ public class SentryReachability: NSObject {
7765 }
7866#endif // DEBUG || SENTRY_TEST || SENTRY_TEST_CI
7967
80- if #available( iOS 12 . 0 , macOS 10 . 14 , tvOS 12 . 0 , visionOS 1 . 0 , watchOS 5 . 0 , * ) {
81- // If we don't use the main queue to start the monitor, the app seems to freeze on iOS 14.8 (SauceLabs)
82- // Also do it async to avoid blocking the main thread
83- // Right now `SentryDispatchQueueWrapper.dispatchAsyncOnMainQueue` is not used because the SDK starts on the main thread,
84- // thus the block is executed in the main thread, not async.
85- DispatchQueue . main. async { [ weak self] in
86- let monitor = NWPathMonitor ( )
87- self ? . pathMonitor = monitor
88- monitor. pathUpdateHandler = self ? . pathUpdateHandler
89- monitor. start ( queue: self ? . reachabilityQueue ?? DispatchQueue . global ( qos: . background) )
90- }
91- } else {
92- // For iOS 11 and earlier, simulate always being connected via WiFi
93- SentrySDKLog . warning ( " NWPathMonitor not available. Using fallback: always connected via WiFi " )
94- reachabilityQueue. async { [ weak self] in
95- self ? . connectivityCallback ( . wiFi)
96- }
97- }
68+ let monitor = NWPathMonitor ( )
69+ self . pathMonitor = monitor
70+ monitor. pathUpdateHandler = self . pathUpdateHandler
71+ monitor. start ( queue: self . reachabilityQueue)
9872 }
9973
10074 @objc ( removeObserver: )
@@ -134,18 +108,15 @@ public class SentryReachability: NSObject {
134108 currentConnectivity = . none
135109
136110 // Clean up NWPathMonitor
137- if #available( iOS 12 . 0 , macOS 10 . 14 , tvOS 12 . 0 , visionOS 1 . 0 , watchOS 5 . 0 , * ) {
138- if let monitor = pathMonitor as? NWPathMonitor {
139- SentrySDKLog . debug ( " Stopping NWPathMonitor " )
140- monitor. cancel ( )
141- pathMonitor = nil
142- }
111+ if let monitor = pathMonitor {
112+ SentrySDKLog . debug ( " Stopping NWPathMonitor " )
113+ monitor. cancel ( )
114+ pathMonitor = nil
143115 }
144116
145117 SentrySDKLog . debug ( " Cleaning up reachability queue. " )
146118 }
147119
148- @available ( iOS 12 . 0 , macOS 10 . 14 , tvOS 12 . 0 , visionOS 1 . 0 , watchOS 5 . 0 , * )
149120 private func pathUpdateHandler( _ path: NWPath ) {
150121 SentrySDKLog . debug ( " SentryPathUpdateHandler called with path status: \( path. status) " )
151122
@@ -160,7 +131,6 @@ public class SentryReachability: NSObject {
160131 connectivityCallback ( connectivity)
161132 }
162133
163- @available ( iOS 12 . 0 , macOS 10 . 14 , tvOS 12 . 0 , visionOS 1 . 0 , watchOS 5 . 0 , * )
164134 private func connectivityFromPath( _ path: NWPath ) -> SentryConnectivity {
165135 guard path. status == . satisfied else {
166136 return . none
@@ -220,20 +190,19 @@ public class SentryReachability: NSObject {
220190
221191// MARK: - Test utils
222192#if DEBUG || SENTRY_TEST || SENTRY_TEST_CI
223- @available ( iOS 12 . 0 , macOS 10 . 14 , tvOS 12 . 0 , visionOS 1 . 0 , watchOS 5 . 0 , * )
224193extension SentryReachability {
225- @ objc public func setReachabilityIgnoreActualCallback( _ value: Bool ) {
194+ func setReachabilityIgnoreActualCallback( _ value: Bool ) {
226195 SentrySDKLog . debug ( " Setting ignore actual callback to \( value) " )
227196 ignoreActualCallback = value
228197 }
229198
230- @ objc public func triggerConnectivityCallback( _ connectivity: SentryConnectivity ) {
199+ func triggerConnectivityCallback( _ connectivity: SentryConnectivity ) {
231200 connectivityCallback ( connectivity)
232201 }
233202}
234203
235- @ _spi ( Private ) @ objc public class SentryReachabilityTestHelper : NSObject {
236- @ objc static public func stringForSentryConnectivity( _ type: SentryConnectivity ) -> String {
204+ class SentryReachabilityTestHelper : NSObject {
205+ static func stringForSentryConnectivity( _ type: SentryConnectivity ) -> String {
237206 type. toString ( )
238207 }
239208}
0 commit comments