瀏覽代碼

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 年之前
父節點
當前提交
176652010c

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

@@ -23,4 +23,9 @@ extern NSString *const kFLEXNetworkObserverEnabledStateChangedNotification;
 + (void)setEnabled:(BOOL)enabled;
 + (void)setEnabled:(BOOL)enabled;
 + (BOOL)isEnabled;
 + (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
 @end

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

@@ -20,6 +20,7 @@
 #import <dispatch/queue.h>
 #import <dispatch/queue.h>
 
 
 NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNetworkObserverEnabledStateChangedNotification";
 NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNetworkObserverEnabledStateChangedNotification";
+static NSString *const kFLEXNetworkObserverEnableOnLaunchDefaultsKey = @"com.flex.FLEXNetworkObserver.enableOnLaunch";
 
 
 @interface FLEXInternalRequestState : NSObject
 @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
 #pragma mark - Statics
 
 
 + (instancetype)sharedObserver
 + (instancetype)sharedObserver