Skip to content

Instantly share code, notes, and snippets.

@iosdevzone
Created May 30, 2013 08:17

Revisions

  1. iosdevzone created this gist May 30, 2013.
    22 changes: 22 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #define S(_s) @selector(application##_s:) // selector generator
    #define N(_n) UIApplication##_n##Notification // name generator
    #define L(X) X(WillResignActive), X(DidEnterBackground), \
    X(WillEnterForeground), X(DidBecomeActive), X(WillTerminate) // apply X over list
    #define C(_a) sizeof((_a))/sizeof((_a)[0]) // array count

    - (void)monitorAppState:(BOOL)start
    {

    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
    SEL s[] = { L(S) };
    NSString* n[] = { L(N) };
    NSAssert(C(n) == C(s), @"Name and selector arrays have same size.");
    for(NSUInteger i = 0; i < C(n); ++i)
    if([self respondsToSelector:s[i]])
    {
    if(start)
    [nc addObserver:self selector:s[i] name:n[i] object:nil];
    else
    [nc removeObserver:self name:n[i] object:nil];
    }
    }