Просмотр исходного кода

Add API to the network observer to enable on launch.

The setting is persisted in NSUserDefaults, so it allows user to choose to leave FLEX network debugging on for longer than the current session.
Ryan Olson лет назад: 11
Родитель
Сommit
176652010c

+ 5 - 0
Classes/Network/PonyDebugger/FLEXNetworkObserver.h

@@ -23,4 +23,9 @@ extern NSString *const kFLEXNetworkObserverEnabledStateChangedNotification;
 + (void)setEnabled:(BOOL)enabled;
 + (BOOL)isEnabled;
 
+/// The enable on launch setting is persisted accross launches of the app.
+/// If YES, the observer will automatically enable itself early in the application lifecycle.
++ (void)setShouldEnableOnLaunch:(BOOL)shouldEnableOnLaunch;
++ (BOOL)shouldEnableOnLaunch;
+
 @end

+ 21 - 0
Classes/Network/PonyDebugger/FLEXNetworkObserver.m

@@ -20,6 +20,7 @@
 #import <dispatch/queue.h>
 
 NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNetworkObserverEnabledStateChangedNotification";
+static NSString *const kFLEXNetworkObserverEnableOnLaunchDefaultsKey = @"com.flex.FLEXNetworkObserver.enableOnLaunch";
 
 @interface FLEXInternalRequestState : NSObject
 
@@ -99,6 +100,26 @@ NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNet
     }
 }
 
++ (void)setShouldEnableOnLaunch:(BOOL)shouldEnableOnLaunch
+{
+    [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:kFLEXNetworkObserverEnableOnLaunchDefaultsKey];
+}
+
++ (BOOL)shouldEnableOnLaunch
+{
+    return [[[NSUserDefaults standardUserDefaults] objectForKey:kFLEXNetworkObserverEnableOnLaunchDefaultsKey] boolValue];
+}
+
++ (void)load
+{
+    // We don't want to do the swizzling from +load because not all the classes may be loaded at this point.
+    dispatch_async(dispatch_get_main_queue(), ^{
+        if ([self shouldEnableOnLaunch]) {
+            [self setEnabled:YES];
+        }
+    });
+}
+
 #pragma mark - Statics
 
 + (instancetype)sharedObserver