Skip Navigation

Add the event-handler skeleton using notifications

The application-delegate object
manages the
app life cycle using events. You can decide how to implement the life cycle of events; the steps below provide an example that is based loosely on the
Breadcrumbs sample app (with differences in declarations and other key areas). Several of the sample apps included in the SDK also demonstrate the event handling lifecycle.
  1. Specify the entitlement ID (
    ) and entitlement version (
    ) in the
    file. See Using an entitlement ID and version to uniquely identify a BlackBerry Dynamics app.
  2. Add the
    dictionary with key
    and its Boolean value set to
    <plist version="1.0"> <dict> ... <key>BlackBerryDynamics</key> <dict> <key>CheckEventReceiver</key> <false/> </dict> ... </dict> </plist>
  3. Import
    . This example relies on a boolean variable named
    . An alternative approach is to declare the variable in your project's
    // AppDelegate.h #import <UIKit/UIKit.h> #import <GD/GDiOS.h> #import <GD/GDState.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> { BOOL hasAuthorized; } @property (strong, nonatomic) UIWindow *window; - (void)handleStateChange:(GDState *)state; - (void)onAuthorized; - (void)onNotAuthorized:(GDAppResultCode)code; @end
  4. Build the app and verify that there are no warnings or errors.
  5. Make your
    an observer for state changing notifications.
    // AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self addStateObserverUsingNotificationCenter]; hasAuthorized = NO; [[GDiOS sharedInstance] authorize]; return YES; } #pragma mark - BlackBerry Dynamics observer for state changes (Notification Center) - (void)addStateObserverUsingNotificationCenter { // register to receive notification center notifications for GD state changes [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveStateChangeNotification:) name:GDStateChangeNotification object:nil]; } - (void) receiveStateChangeNotification:(NSNotification *) notification { if ([[notification name] isEqualToString:GDStateChangeNotification]) { NSDictionary* userInfo = [notification userInfo]; NSString *propertyName = [userInfo objectForKey:GDStateChangeKeyProperty]; GDState* state = [userInfo objectForKey:GDStateChangeKeyCopy]; // For the purposes of this example, we want to log a different message so that it is known // these calls are coming from Notification Center if ([propertyName isEqualToString:GDKeyIsAuthorized]) { NSLog(@"receiveStateChangeNotification - isAuthorized: %@", state.isAuthorized ? @"true" : @"false"); [self handleStateChange:state]; } else if ([propertyName isEqualToString:GDKeyReasonNotAuthorized]) { NSLog(@"receiveStateChangeNotification - reasonNotAuthorized: %ld", (long) state.reasonNotAuthorized); } else if ([propertyName isEqualToString:GDKeyUserInterfaceState]) { NSLog(@"receiveStateChangeNotification - userInterfaceState: %ld", (long) state.userInterfaceState); } else if ([propertyName isEqualToString:GDKeyCurrentScreen]) { NSLog(@"receiveStateChangeNotification - currentScreen: %ld", (long) state.currentScreen); } } }
  6. Use
    to handle the state of the
    BlackBerry Dynamics Runtime
    // AppDelegate.m #pragma mark - Good Dynamics handler methods for state changes - (void)handleStateChange:(GDState *)state { if (state.isAuthorized) { NSLog(@"gdState: authorized"); [self onAuthorized]; } else { GDAppResultCode errorCode = [state reasonNotAuthorized]; NSLog(@"gdState: not authorized, error:%ld", (long) errorCode); [self onNotAuthorized:errorCode]; } } - (void)onAuthorized { if (!hasAuthorized) { // launch application UI here hasAuthorized = YES; } } - (void)onNotAuthorized:(GDAppResultCode)code { /* Handle the Good Libraries not authorized event. */ switch (code) { case GDErrorActivationFailed: case GDErrorProvisioningFailed: case GDErrorPushConnectionTimeout: case GDErrorSecurityError: case GDErrorAppDenied: case GDErrorAppVersionNotEntitled: case GDErrorBlocked: case GDErrorWiped: case GDErrorRemoteLockout: case GDErrorPasswordChangeRequired: { // an condition has occured denying authorization, an application may wish to log these events NSLog(@"gdState: not authorized, error:%ld", (long) code); break; } case GDErrorIdleLockout: { // idle lockout is benign & informational break; } default: NSAssert(false, @"Unhandled not authorized event"); break; } }
  7. Add
    to the
    Copy Bundle Resources
    build phase in
    . Verify that the copy of the bundle that is distributed with the framework has been added to the project.