Creating the SUP101CallbackHandler File

Goal: Configure the SUP101CallBackHandler file.

Prerequisites

SUP101CallbackHandler is a subclass of SUPCallbackHandler, and is used to listen for events sent from the server. The header, SUP101CallbackHandler.h, is referenced in a number of classes in this application, so you create it first.

There are two threads involved in the SUP101 application — the main thread, which is driven by the client application user interface controller, and the mobile object client access thread, which takes charge of message transportation with the server and synchronization with the application through the mobile object. In iOS, all code that updates the user interface must be called on the main thread, so it is a good idea to send notifications that might trigger changes to the interface from the main thread.

Tip: Be sure you saved the SUP_iOS_Custom_Dev_Tutorial_code.zip file to your development machine so you can easily copy and paste the provided code into the corresponding .h and .m files in Xcode.
Task
  1. In the SUP101 Xcode project, select File > New > New File.
  2. Select Objective-C Class and click Next.
  3. Enter SUPDefaultCallbackHandler in the Subclass of field and click Next.
  4. Enter SUP101CallbackHandler in the Save as field and click Save.
    The files SUP101CallbackHandler.h and SUP101CallbackHandler.m are created in the Project Navigator.
  5. Click the SUP101CallbackHandler.h file and replace the existing code with the provided source code.
    #import "SUPDefaultCallbackHandler.h"
    
    // These strings will be used to send out NSNotifications.
    #define ON_IMPORT_SUCCESS @"SUPImportSuccess"
    #define ON_LOGIN_SUCCESS @"SUPLoginSuccess"
    #define ON_LOGIN_FAILURE @"SUPLoginFailure"
    #define ON_CONNECT_SUCCESS @"SUPConnectSuccess"
    #define ON_CONNECT_FAILURE @"SUPConnectFailure"
    #define ON_REPLAY_SUCCESS @"SUPReplaySuccess"
    #define ON_REPLAY_FAILURE @"SUPReplayFailure"
    
    // For this example we are only handling a small subset of the notifications
    // defined in SUPCallbackHandler.  Refer to the iOS Developer's Guide for SUP for more
    // information on when the other callbacks are used.
    
    @interface SUP101CallbackHandler : SUPDefaultCallbackHandler
    {
    }
    
    @end
    
  6. Click the SUP101CallbackHandler.m file and replace the existing code with the provided source code.
    #import "SUP101CallbackHandler.h"
    
    @implementation SUP101CallbackHandler
    
    - (void)sendNotification:(NSNotification *)notification
    {
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        [notification release];
    }
    
    - (void)postNotification:(NSString *)notification withObject:(id)obj;
    {
        // All callback notifications other than onSubscribe: will happen on a thread other than the main UI thread. So, if you
        // want to update the UI in response to a callback you need to post the notification from the main thread.
        NSNotification *n = [NSNotification notificationWithName:notification object:obj];
        [n retain];
        [self performSelectorOnMainThread:@selector(sendNotification:) withObject:n waitUntilDone:NO];
    }
    
    - (void)onConnectionStatusChange:(SUPDeviceConnectionStatus)connStatus :(SUPDeviceConnectionType)connType :(int32_t)errorCode :(NSString *)errorString
    {
        NSString *notification = nil;
        switch(connStatus)
        {
            case CONNECTED_NUM:
                notification = ON_CONNECT_SUCCESS;
                break;
            case DISCONNECTED_NUM:
                notification = ON_CONNECT_FAILURE;
                break;
            default:
                // Ignore all other status changes for this example.
                break;
        }
        
        if (notification != nil) [self postNotification:notification withObject:nil];
    }
    
    - (void)onReplaySuccess:(id)theObject
    {
        MBOLogInfo(@"================================================");
        MBOLogInfo(@"Replay Successful");
        MBOLogInfo(@"=================================================");
        
        [self postNotification:ON_REPLAY_SUCCESS withObject:theObject];
    }
    
    - (void)onReplayFailure:(id)theObject
    {
        MBOLogInfo(@"================================================");
        MBOLogInfo(@"Replay Failure");
        MBOLogInfo(@"=================================================");
        
        [self postNotification:ON_REPLAY_FAILURE withObject:theObject];
    }
    
    - (void)onLoginSuccess
    {
        MBOLogInfo(@"================================================");
        MBOLogInfo(@"Login Successful");
        MBOLogInfo(@"=================================================");
        
        [self postNotification:ON_LOGIN_SUCCESS withObject:nil];
    }
    
    - (void)onLoginFailure
    {
    	MBOLog(@"=============================");
    	MBOLogError(@"Login Failed");
    	MBOLog(@"=============================");
        
    	[self postNotification:ON_LOGIN_FAILURE withObject:nil];
    }
    
    - (void)onSubscribeSuccess
    {
        MBOLogInfo(@"================================================");
        MBOLogInfo(@"Subscribe Successful");
        MBOLogInfo(@"=================================================");
    }
    
    - (void)onImportSuccess
    {
        MBOLogInfo(@"================================================");
        MBOLogInfo(@"import ends Successful");
        MBOLogInfo(@"=================================================");
        
        [self postNotification:ON_IMPORT_SUCCESS withObject:nil];
    }
    
    @end