Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
Use notifications rather than querying state to allow Tweaks to be us…
Browse files Browse the repository at this point in the history
…ed in app extensions.
  • Loading branch information
grp committed Jan 29, 2015
1 parent 2fef4c9 commit 428c588
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion FBTweak/FBTweakShakeWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@

@implementation FBTweakShakeWindow {
BOOL _shaking;
BOOL _active;
}

- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// Maintain this state manually using notifications so Tweaks can be used in app extensions, where UIApplication is unavailable.
_active = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationWillResignActiveWithNotification:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationDidBecomeActiveWithNotification:) name:UIApplicationDidBecomeActiveNotification object:nil];
}

return self;
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)_applicationWillResignActiveWithNotification:(NSNotification *)notification
{
_active = NO;
}

- (void)_applicationDidBecomeActiveWithNotification:(NSNotification *)notification
{
_active = YES;
}

- (void)tweakViewControllerPressedDone:(FBTweakViewController *)tweakViewController
Expand Down Expand Up @@ -46,7 +75,7 @@ - (BOOL)_shouldPresentTweaks
#if TARGET_IPHONE_SIMULATOR && FB_TWEAK_ENABLED
return YES;
#elif FB_TWEAK_ENABLED
return _shaking && [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
return _shaking && _active;
#else
return NO;
#endif
Expand Down

0 comments on commit 428c588

Please sign in to comment.