Converting Your Mobile App to the Mobile Cloud

Preview:

DESCRIPTION

This presentation looks at the process to convert an existing IOS application which stores it's data on the phone to one that stores the information in the cloud. While IBMs MobileFirst cloud software is used the implementation is applicable to other cloud storage mechanism.

Citation preview

Converting Your Mobile App to the Mobile Cloud

Roger BrinkleyDeveloper Evangelist

The Connected Experience

• Modern mobile consumers require a connected environment– Sharing of information with others– Multiple device access– Security– Safe Storage– Magnitude of Data

Enter IBM MobileFirst

Converting an Existing Mobile App

• Create a Mobile Cloud application on Bluemix• Install and configure the SDKS• Add an application delegate• Modify the Model to Use IBM Data service• Modify ViewController to list/persist data

from MBaaS

Create Mobile Cloud App on BlueMix

Install and Configure the SDK

• Download the iOS SDK• Add IBM Frameworks to your apps

frameworks• Modify your .plist file

Modify Application Delegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSString *applicationId = nil; NSString *applicationSecret = nil; NSString *applicationRoute = nil; BOOL hasValidConfiguration = YES; NSString *errorMessage = @""; // Read the applicationId from the bluelist.plist. NSString *configurationPath = [[NSBundle mainBundle] pathForResource:@"bluelist" ofType:@"plist"]; if(configurationPath){ NSDictionary *configuration = [[NSDictionary alloc] initWithContentsOfFile:configurationPath]; applicationId = [configuration objectForKey:@"applicationId"]; if(!applicationId || [applicationId isEqualToString:@""]){ hasValidConfiguration = NO; errorMessage = @"Open the bluelist.plist and set the applicationId to the Bluemix applicationId"; } applicationSecret = [configuration objectForKey:@"applicationSecret"]; if(!applicationSecret || [applicationSecret isEqualToString:@""]){ hasValidConfiguration = NO; errorMessage = @"Open the bluelist.plist and set the applicationSecret with your Bluemix application's secret"; } applicationRoute = [configuration objectForKey:@"applicationRoute"]; if(!applicationRoute || [applicationRoute isEqualToString:@""]){ hasValidConfiguration = NO; errorMessage = @"Open the bluelist.plist and set the applicationRoute to the Bluemix application's route"; } }

Modify Application Delegate(2) if(hasValidConfiguration){ // Initialize the SDK and Bluemix services

[IBMBluemix initializeWithApplicationId:applicationId andApplicationSecret:applicationSecret andApplicationRoute:applicationRoute]; [IBMData initializeService];}else{ [NSException raise:@"InvalidApplicationConfiguration" format: @"%@", errorMessage]; } return YES;

Modify Model to Use IBM Data

• Simple apps convert NSObject to IBMDataObject in the include file

#import <IBMData/IBMData.h>

@interface IBM_Item : IBMDataObject <IBMDataObjectSpecialization>

• Implement the dataClassName method and register the specialization

@dynamic name;

+(void) initialize{ [self registerSpecialization];}

+(NSString*) dataClassName{ return @"Item";}

Modify the ViewController(listItem)

- (void)listItems: (void(^)(void)) cb{ [self reloadLocalTableData]; if(cb){ cb(); }}

Modify the ViewController(listItem)

- (void)listItems: (void(^)(void)) cb{

IBMQuery *qry = [IBM_Item query]; [[qry find] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"listItems failed with error: %@", task.error); } else { self.itemList = [NSMutableArray arrayWithArray: task.result]; [self reloadLocalTableData]; if(cb){ cb(); } } return nil; }];}

Modify ViewController(createItem)- (void) createItem: (IBM_Item*) item{ [self.itemList addObject: item];}

- (void) createItem: (IBM_Item*) item{ [self.itemList addObject: item]; [self reloadLocalTableData]; [[item save] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"createItem failed with error: %@", task.error); }return nil; }]; }

Modify ViewController(updateItem)

- (void) updateItem: (IBM_Item*) item{ self.editedCell.textLabel.text = item.name;}

- (void) updateItem: (IBM_Item*) item{ self.editedCell.textLabel.text = item.name; [[item save] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"updateItem failed with error: %@", task.error); }return nil; }]; }

Update ViewController(deleteItem)-(void) deleteItem: (IBM_Item*) item{ [self.itemList removeObject: item]; [self listItems: nil]; // Exit edit mode to avoid need to click Done button [self.tableView setEditing:NO animated:YES];}

-(void) deleteItem: (IBM_Item*) item{ [self.itemList removeObject: item]; [self reloadLocalTableData]; [[item delete] continueWithBlock:^id(BFTask *task) { if(task.error){ NSLog(@"deleteItem failed with error: %@", task.error); } else { [self listItems: nil]; }return nil; }]; // Exit edit mode to avoid need to click Done button [self.tableView setEditing:NO animated:YES];}

Demo

Moving Forward with Push Notification

• Get Andriod or iOS SSL Certificate and Keys• Enter the SSL Certificate in apps push service• Install the SDK• Modify the AppDelegate• Modify the ViewController

Enter the SSL Certificate in Bluemix

Install the SDK

• Add IBM Frameworks to your apps frameworks– IBMPush.framework– IBMCloudCode.framework

Modify the AppDelegate

// This will be used to refresh the list upon receiving a Push notification@property IBM_ListViewController *listViewController;

if(hasValidConfiguration){ // Initialize the SDK and Bluemix services

[IBMBluemix initializeWithApplicationId:applicationId andApplicationSecret:applicationSecret andApplicationRoute:applicationRoute]; [IBMData initializeService]; /* New code for push services */ [IBMCloudCode initializeService]; [IBMPush initializeService]; [IBMLogger addLogCategory: @"TRACE"]; [IBMLogger addLogCategory: @"DEBUG"]; [IBMLogger addLogCategory: @"ERROR"]; [IBMLogger addLogCategory: @"WARNING"]; NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; NSLog(@"bundle: %@", bundleIdentifier); // Register application for push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

Modify the ViewController

• Add a listItems callback to header • Add an IBMCloudCode service property• Connect the AppDelegate to ViewController

and initialize CloudService• Modify when Item is created, updated,

deleted

Add a listItems Callback to Header

- (void)listItems: (void(^)(void)) cb;

Add a IBMCloudCode Service Property

@property IBMCloudCode *cloudCodeService;

Connect the AppDelegate to ViewController and Initialize

// Setting the AppDelegate's connection to this ViewController IBM_AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate setListViewController: self]; // Initialize cloud code service self.cloudCodeService = [IBMCloudCode service];

Modify When Item is Created

[[item save] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"createItem failed with error: %@", task.error); }

//save the new item and then notify other devices via cloud code for push notifications [[[item save] continueWithSuccessBlock:^id(BFTask *task) { return [self.cloudCodeService post:@"notifyOtherDevices" withDataPayload:nil withHeaders:nil]; }] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"createItem failed with error: %@", task.error); } return nil; }];

Modify When Item is Updated

[[item save] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"updateItem failed with error: %@", task.error); }

//save the updated item and then notify other devices via cloud code for push notifications [[[item save] continueWithSuccessBlock:^id(BFTask *task) { NSLog(@"in continueWithSuccessBlock"); return [self.cloudCodeService post:@"notifyOtherDevices" withDataPayload:nil withHeaders:nil]; }] continueWithBlock:^id(BFTask *task) { NSLog(@"in continueWithBlock"); if(task.error) { NSLog(@"updateItem failed with error: %@", task.error); } return nil;

Modify When Item is Deleted

[[item delete] continueWithBlock:^id(BFTask *task) { if(task.error){ NSLog(@"deleteItem failed with error: %@", task.error); } else { [self listItems: nil]; } [[[item delete] continueWithSuccessBlock:^id(BFTask *task) { return [self.cloudCodeService post:@"notifyOtherDevices" withDataPayload:nil withHeaders:nil]; }] continueWithBlock:^id(BFTask *task) { if(task.error) { NSLog(@"deleteItem failed with error: %@", task.error); }

Demo

Recommended