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.

The delegate file extends 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. Add the view controller property to the application delegate:
    1. Open the SUPAppdelegate.h file and add this code:
      @interface SUP101AppDelegate : NSObject <UIApplicationDelegate> {
          UIWindow *window;
          UINavigationController *navController;
      }
      
      @property (nonatomic, retain) IBOutlet UIWindow *window;
      @property (nonatomic, retain) IBOutlet UINavigationController *navController;
      - (void)showNoTransportAlert:(NSInteger) ret;
      @end
      
    2. Save the SUP101Appdelegate.h file.
  2. Create an instance of the view controller and 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:
    1. In the SUP101 Xcode project, open the SUP101Appdelegate.m file and enter this code:
      #import "SUP101AppDelegate.h"
      #import "SUPMessageClient.h"
      #import "SUP101_SUP101DB.h"
      #import "SUP101CallbackHandler.h"
      
      @implementation SUP101AppDelegate
      
      @synthesize window;
      @synthesize navController;
      
      - (void)showNoTransportAlert:(NSInteger) ret
      {
          NSString *message = nil;
          if (ret == kSUPMessageClientNoSettings) {
              message = [[NSString alloc] initWithString:@"No required settings, use the Settings app to enter the provisioning information."];
          } else if (ret == kSUPMessageClientKeyNotAvailable) {
              message = [[NSString alloc] initWithString:@"Unable to access the key."];
          } else {
              message = [[NSString alloc] initWithString:@"Operation fails."];
          }
          UIAlertView * noTransportAlert = [[[UIAlertView alloc] initWithTitle:@"Messaging Client Fails to Start" message:message delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"Label for alert button OK") otherButtonTitles:nil] autorelease];
          [noTransportAlert show];
      }
      - (void)applicationDidFinishLaunching:(UIApplication *)application 
      {
      	// Override point for customization after application launch	
      	if([SUP101_SUP101DB databaseExists])
      	   [SUP101_SUP101DB deleteDatabase];
      	[SUP101_SUP101DB createDatabase];
      
      	[MBOLogger setLogLevel:LOG_INFO]; 
      	SUP101CallbackHandler* databaseCH = [SUP101CallbackHandler newInstance];
      	[SUP101_SUP101DB registerCallbackHandler:databaseCH]; 
      	[SUP101_SUP101DB createDatabase];
      	[SUP101_SUP101DB startBackgroundSynchronization]; 
      	[NSThread sleepForTimeInterval:2];
      	
      	NSInteger result = [SUPMessageClient start];
      	
      	if (result == kSUPMessageClientSuccess) 
      	{
      		[SUP101_SUP101DB asyncOnlineLogin:@"supuser" password:@"s3pUser"]; 
      		while([databaseCH loginSuccessCount] < 1) {
      			[NSThread sleepForTimeInterval:1];
      		}
      		[window addSubview:navController.view]; 
      		[window makeKeyAndVisible];
      	} else 
      	{ 
      		[self showNoTransportAlert:result];
      	}
      }
      - (void)applicationWillTerminate:(UIApplication *)application
      {
          [SUP101_SUP101DB unsubscribe];
          [SUPMessageClient stop];
      
      }
      
      - (void)dealloc {
          [navController release];
          [window release];
          [super dealloc];
      }
      - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
      	
      	//button index 0 is the cancel button
      	if (buttonIndex == 0){
      		exit (0);
      	} 
      }
      @end
      
    2. Save the SUP101Appdelegate.m file.