converting your mobile app to the mobile cloud

27
Converting Your Mobile App to the Mobile Cloud Roger Brinkley Developer Evangelist

Upload: roger-brinkley

Post on 21-Dec-2014

96 views

Category:

Software


0 download

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.

TRANSCRIPT

Page 1: Converting Your Mobile App to the Mobile Cloud

Converting Your Mobile App to the Mobile Cloud

Roger BrinkleyDeveloper Evangelist

Page 2: Converting Your Mobile App to the Mobile Cloud

The Connected Experience

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

Page 3: Converting Your Mobile App to the Mobile Cloud

Enter IBM MobileFirst

Page 4: Converting Your Mobile App to the Mobile Cloud

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

Page 5: Converting Your Mobile App to the Mobile Cloud

Create Mobile Cloud App on BlueMix

Page 6: Converting Your Mobile App to the Mobile Cloud

Install and Configure the SDK

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

frameworks• Modify your .plist file

Page 7: Converting Your Mobile App to the Mobile Cloud

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"; } }

Page 8: Converting Your Mobile App to the Mobile Cloud

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;

Page 9: Converting Your Mobile App to the Mobile Cloud

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";}

Page 10: Converting Your Mobile App to the Mobile Cloud

Modify the ViewController(listItem)

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

Page 11: Converting Your Mobile App to the Mobile Cloud

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; }];}

Page 12: Converting Your Mobile App to the Mobile Cloud

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; }]; }

Page 13: Converting Your Mobile App to the Mobile Cloud

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; }]; }

Page 14: Converting Your Mobile App to the Mobile Cloud

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];}

Page 15: Converting Your Mobile App to the Mobile Cloud

Demo

Page 16: Converting Your Mobile App to the Mobile Cloud

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

Page 17: Converting Your Mobile App to the Mobile Cloud

Enter the SSL Certificate in Bluemix

Page 18: Converting Your Mobile App to the Mobile Cloud

Install the SDK

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

Page 19: Converting Your Mobile App to the Mobile Cloud

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)];

Page 20: Converting Your Mobile App to the Mobile Cloud

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

Page 21: Converting Your Mobile App to the Mobile Cloud

Add a listItems Callback to Header

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

Page 22: Converting Your Mobile App to the Mobile Cloud

Add a IBMCloudCode Service Property

@property IBMCloudCode *cloudCodeService;

Page 23: Converting Your Mobile App to the Mobile Cloud

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];

Page 24: Converting Your Mobile App to the Mobile Cloud

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; }];

Page 25: Converting Your Mobile App to the Mobile Cloud

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;

Page 26: Converting Your Mobile App to the Mobile Cloud

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); }

Page 27: Converting Your Mobile App to the Mobile Cloud

Demo