15
15
#import " FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.h"
16
16
17
17
#import < Foundation/Foundation.h>
18
+ #import < Network/Network.h>
18
19
#import < UIKit/UIKit.h>
19
20
20
21
#import " FirebasePerformance/Sources/AppActivity/FPRSessionManager.h"
@@ -55,6 +56,15 @@ @interface FPRAppActivityTracker ()
55
56
/* * Current running state of the application. */
56
57
@property (nonatomic , readwrite ) FPRApplicationState applicationState;
57
58
59
+ /* * Current network connection type of the application. */
60
+ @property (nonatomic , readwrite ) firebase_perf_v1_NetworkConnectionInfo_NetworkType networkType;
61
+
62
+ /* * Network monitor object to track network movements. */
63
+ @property (nonatomic , readwrite ) nw_path_monitor_t monitor;
64
+
65
+ /* * Queue used to track the network monitoring changes. */
66
+ @property (nonatomic , readwrite ) dispatch_queue_t monitorQueue;
67
+
58
68
/* * Trace to measure the app start performance. */
59
69
@property (nonatomic ) FIRTrace *appStartTrace;
60
70
@@ -122,9 +132,12 @@ + (instancetype)sharedInstance {
122
132
*/
123
133
- (instancetype )initAppActivityTracker {
124
134
self = [super init ];
125
- _applicationState = FPRApplicationStateUnknown;
126
- _appStartGaugeMetricDispatched = NO ;
127
- _configurations = [FPRConfigurations sharedInstance ];
135
+ if (self != nil ) {
136
+ _applicationState = FPRApplicationStateUnknown;
137
+ _appStartGaugeMetricDispatched = NO ;
138
+ _configurations = [FPRConfigurations sharedInstance ];
139
+ [self startTrackingNetwork ];
140
+ }
128
141
return self;
129
142
}
130
143
@@ -147,6 +160,35 @@ - (FIRTrace *)activeTrace {
147
160
return self.backgroundSessionTrace ;
148
161
}
149
162
163
+ - (void )startTrackingNetwork {
164
+ self.networkType = firebase_perf_v1_NetworkConnectionInfo_NetworkType_NONE;
165
+
166
+ if (@available (iOS 12 , tvOS 12 , *)) {
167
+ dispatch_queue_attr_t attrs = dispatch_queue_attr_make_with_qos_class (
168
+ DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, DISPATCH_QUEUE_PRIORITY_DEFAULT);
169
+ self.monitorQueue = dispatch_queue_create (" com.google.firebase.perf.network.monitor" , attrs);
170
+
171
+ self.monitor = nw_path_monitor_create ();
172
+ nw_path_monitor_set_queue (self.monitor , self.monitorQueue );
173
+ __weak FPRAppActivityTracker *weakSelf = self;
174
+ nw_path_monitor_set_update_handler (self.monitor , ^(nw_path_t _Nonnull path) {
175
+ BOOL isWiFi = nw_path_uses_interface_type (path, nw_interface_type_wifi);
176
+ BOOL isCellular = nw_path_uses_interface_type (path, nw_interface_type_cellular);
177
+ BOOL isEthernet = nw_path_uses_interface_type (path, nw_interface_type_wired);
178
+
179
+ if (isWiFi) {
180
+ weakSelf.networkType = firebase_perf_v1_NetworkConnectionInfo_NetworkType_WIFI;
181
+ } else if (isCellular) {
182
+ weakSelf.networkType = firebase_perf_v1_NetworkConnectionInfo_NetworkType_MOBILE;
183
+ } else if (isEthernet) {
184
+ weakSelf.networkType = firebase_perf_v1_NetworkConnectionInfo_NetworkType_ETHERNET;
185
+ }
186
+ });
187
+
188
+ nw_path_monitor_start (self.monitor );
189
+ }
190
+ }
191
+
150
192
/* *
151
193
* Checks if the prewarming feature is available on the current device.
152
194
*
@@ -286,6 +328,10 @@ - (void)appWillResignActiveNotification:(NSNotification *)notification {
286
328
}
287
329
288
330
- (void )dealloc {
331
+ if (@available (iOS 12 , tvOS 12 , *)) {
332
+ nw_path_monitor_cancel (self.monitor );
333
+ }
334
+
289
335
[[NSNotificationCenter defaultCenter ] removeObserver: self
290
336
name: UIApplicationDidBecomeActiveNotification
291
337
object: [UIApplication sharedApplication ]];
0 commit comments