iOS Tutorial iOS Tutorial Part IV

View the Project on GitHub FatFractal/hoodyoodoo

Update the Client Code

In order to use this new extension method, you need to do add a matching StatsObject to your project.

#import 
@interface StatsObject : NSObject {
    NSNumber        *totalUsers;
    NSNumber        *totalCelebrities;
    NSNumber        *totalRatings;
    NSNumber        *yourRatings;
}

@property (strong, nonatomic) NSNumber          *totalUsers;
@property (strong, nonatomic) NSNumber          *totalCelebrities;
@property (strong, nonatomic) NSNumber          *totalRatings;
@property (strong, nonatomic) NSNumber          *yourRatings;
@end

as well as StatsObject.m

#import "StatsObject.h"
@implementation StatsObject
@synthesize totalUsers, totalCelebrities, totalRatings, yourRatings;
- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (NSString*) description {
    return [[NSString alloc]
    initWithFormat:@"StatsObject[totalUsers[%d] totalCelebrities[%d] totalRatings[%d] yourRatings[%d]]",
    [self totalUsers], [self totalCelebrities], [self totalRatings], [self yourRatings]];
}
@end

You can then call the extension using the standard CRUD url pattern.

statsObject= [[FatFractal main] getObjFromUrl:@"/ff/ext/Stats" error:&error];

We also need to update TopCelebViewController.h to import our new StatsObject class.

#import "StatsObject.h"

And add in the additional properties we will be using:

@property (nonatomic, retain) IBOutlet UILabel *totalUsersLabel;
@property (nonatomic, retain) IBOutlet UILabel *totalCelebritiesLabel;
@property (nonatomic, retain) IBOutlet UILabel *totalRatingsLabel;
@property (nonatomic, retain) IBOutlet UILabel *yourRatingsLabel;

We also need to update TopCelebViewController.m to use these and add a new method to retrieve the data.

First, let’s synthesize the properties we have added:

@synthesize statsObject, totalUsersLabel, totalRatingsLabel, totalCelebritiesLabel, yourRatingsLabel;

And add in a new method to retrieve the data:

-(void) getStats {
    if(![[FatFractal main] loggedIn]) {
        [(AppDelegate *)[[UIApplication sharedApplication] delegate]
        showLoginWithDelegate:self action:@selector(getStats)
        message:@"Please Login"];
    }
    else {
        NSError *error;
        statsObject= [[FatFractal main] getObjFromUrl:@"/ff/ext/Stats" error:&error];
        if (error) {
            NSLog(@"StatsViewController getStats failed: %@", [error localizedDescription]);
            return;
        } else {
            totalUsersLabel.text = [statsObject.totalUsers stringValue];
            totalCelebritiesLabel.text = [statsObject.totalCelebrities stringValue];
            totalRatingsLabel.text = [statsObject.totalRatings stringValue];
            yourRatingsLabel.text = [statsObject.yourRatings stringValue];
        }
    }
}

Now that we have our statsObject, let’s add in the UI components to display the results. To do this, we add additional UILabels for both the headings and the data as follows:

  1. Add two UILabels that will display “Total Users” and the data from totalUsers.
  2. Add two UILabels that will display “Total Celebrities” and the data from totalCelebrities.
  3. Add two UILabels that will display “Total Ratings” and the data from totalRatings.
  4. Add two UILabels that will display “Your Ratings” and the data from yourRatings.

Your scene should look something like this:

Next, make sure and connect up your UIComponents in the MainStoryboard.storyboard  for the TopCelebViewController elements.

totalUsersLabel
Referencing Outlet to TopCelebViewController/totalUsersLabel

totalCelebritiesLabel
Referencing Outlet to TopCelebViewController/totalCelebritiesLabel

totalRatingsLabel
Referencing Outlet to TopCelebViewController/totalRatingsLabel

yourRatingsLabel
Referencing Outlet to TopCelebViewController/yourRatingsLabel

Lastly, we need to initialize these new properties by adding the following to viewDidLoad and call the getStats method:

statsObject = [[StatsObject alloc]init];
totalRatingsLabel.text = nil;
totalCelebritiesLabel.text = nil;
totalUsersLabel.text = nil;
yourRatingsLabel.text = nil;
[self getStats];

That’s it!

Now, as soon as ratings are being captured in WouldYa’s, then we can call the server extension that will aggregate the statistics on the fly and return a StatsObject that can be used to populate the scene.

Final result - successfully created a StatsObject on the API from an server extension that updates the corresponding UI components.

NEXT: Push Notifications