Configuring the SUP101Appdelegate Files

Goal: The SUP101Appdelegate.h and SUP101Appdelegate.m files are created when you create the Xcode project, but you must add the view controller property and create the view controller instance.

Delegates extend the functionality of reusable objects. A delegate allows one object to send messages to another object specified as its delegate to ask for input, or to be notified when an event occurs.

  1. Click the SUP101Appdelegate.h file and replace the existing code with the provided code to add the view controller property to the application delegate:
    #import <UIKit/UIKit.h>
    #import "SUP101CallbackHandler.h"
    
    @interface SUP101AppDelegate : NSObject <UIApplicationDelegate> {
    
    }
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet UINavigationController *navController;
    @property (nonatomic, retain) NSDate *connectStartTime;
    @property (nonatomic, retain) SUP101CallbackHandler *callbackHandler;
    
    @end
    
  2. Click the SUP101Appdelegate.m file and replace the existing code with the provided code to create an instance of the view controller, set it as the value for the property, import the view controller's header file, synthesize the accessor methods, and make sure the view controller is released in the dealloc method:
    #import "SUP101AppDelegate.h"
    #import "SUPMessageClient.h"
    #import "SUP101_SUP101DB.h"
    #import "SUP101CallbackHandler.h"
    
    @implementation SUP101AppDelegate
    
    @synthesize window, navController, connectStartTime, callbackHandler;
    
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        exit (0);
    }
    
    - (void)showNoTransportAlert:(NSInteger) ret
    {
        NSString *message = nil;
        
        if (ret == kSUPMessageClientNoSettings) {
            message = @"The connection settings have not been filled in for this application. Go to Settings, enter the connection information, and restart this app.";
        } else if (ret == kSUPMessageClientKeyNotAvailable) {
            message = @"Unable to access the key.";
        } else {
            message = @"An error occurred attempting to log in.";
        }
        
        UIAlertView * noTransportAlert = [[UIAlertView alloc] initWithTitle:@"Unable to start message server" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [noTransportAlert performSelectorOnMainThread:@selector(show) withObject:self waitUntilDone:YES];
        [noTransportAlert release];
    }
    
    
    -(void)onConnectSuccess:(NSNotification *)obj
    {
        // Connection to the server was made, so log in.
        // See [CallbackHandler onLoginSuccess] and [CallbackHandler onLoginFailure]. One of those
        // callbacks will be called at some point in the future.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:ON_CONNECT_SUCCESS object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:ON_CONNECT_FAILURE object:nil];
        [SUP101_SUP101DB beginOnlineLogin:@"supAdmin" password:@"s3pAdmin"];
    }
    
    -(void)onConnectFailure:(NSNotification *)obj
    {
        // Once [SUPMessageClient start] is called, ON_CONNECT_FAILURE is sent from our callback handler
        // until the device is connected or something changes. If we haven't connected in 30 seconds, give up.
        NSDate *now = [NSDate date];
        if ([now timeIntervalSinceDate:self.connectStartTime] > 30) {
            [SUPMessageClient stop];
            [self showNoTransportAlert:kSUPMessageClientFailure];
        }
    }
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        
        // Override point for customization after application launch
        
        // Register a callback handler. This should be done before any other SUP code is called.
    	self.callbackHandler = [SUP101CallbackHandler new];
    	[SUP101_SUP101DB registerCallbackHandler:self.callbackHandler];
        
        // Don't try to connect if the connection settings have not been set up yet.
        if (![SUPMessageClient provisioned]) {
    		[self showNoTransportAlert:kSUPMessageClientNoSettings];
        } else {        
            // Set log level (optional -- this will generate a lot of output in the debug console)
            [MBOLogger setLogLevel:LOG_INFO];
            
            // Normally you would not delete the local database. For this simple example, though,
            // deleting and creating an empty database will cause all data to be sent from the
            // server, and we can use [CallbackHandler onImportSuccess:] to know when to proceed.
            [SUP101_SUP101DB deleteDatabase];
            [SUP101_SUP101DB createDatabase];
            
            // Start listening for messages from the server.
            [SUP101_SUP101DB startBackgroundSynchronization];
            
            // Start up the messaging client. This will attempt to connect to the server. If a connection was
            // established we can proceed with login. See onConnectFailure: for more information about handling connection failure.
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectSuccess:) name:ON_CONNECT_SUCCESS object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectFailure:) name:ON_CONNECT_FAILURE object:nil];
            self.connectStartTime = [NSDate date];
            [SUPMessageClient start];
            
            // Create the main UI for the application. We will update it as we receive messages from the server.
            [window addSubview:navController.view];
            [window makeKeyAndVisible];
        }   
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // In this example, because we delete and recreate the local database, we need to unsubscribe
        // and shut down the app when it is no longer active.  All data will be sent on next launch.
        [SUP101_SUP101DB unsubscribe];
        [SUPMessageClient stop];
        exit(EXIT_SUCCESS);
    }
    
    - (void)dealloc {
        self.navController = nil;
        self.window = nil;
        self.callbackHandler = nil;
        self.connectStartTime = nil;
        
        [super dealloc];
    }
    
    @end