Now, set up the Scene to play the game. Start by retrieving a random Celebrity that was saved to the API and displaying it on the FirstViewController, which we will refactor to be the WouldYaViewController. What’s a WouldYa, you ask? You'll find out in part II of the Tutorial! But for now... For this Scene, refactor the FirstViewController to be called WouldYaViewController by renaming everything.
Next, modify WouldYaViewController.h as follows:
#import "AppDelegate.h"
#import "Celebrity.h"
@interface WouldYaViewController : UIViewController
@property (strong, nonatomic) Celebrity *leftCelebrity;
- (void) loadCelebrities;
@property (strong, nonatomic) IBOutlet UILabel *leftCelebrityLabel;
@property (strong, nonatomic) IBOutlet UIButton *leftCelebrityButton;
@end
These modifications accomplish the following:
Then, modify the WouldYaViewController.m as follows:
First, synthesize the necessary properties.
@synthesize celebrityList, leftCelebrity, leftCelebrityLabel, leftCelebrityButton;
Next, we want to use a single CRUD method that will retrieve all the objects needed from the API at once.
This line of code returns all objects found at that API collection. We will create the loadCelebrities method to test this out.
- (void) loadCelebrities {
NSError * error;
NSArray *celebArray = [[FatFractal main]
getArrayFromUrl:[NSString stringWithFormat:@"/ff/resources/Celebrity"]
error:&error];
if (error) {
NSLog(@"WouldYaViewController loadCelebrities celebArray failed: %@", [error localizedDescription]);
return;
}
if([celebArray count] == 0) {
[leftCelebrityButton setBackgroundImage:[UIImage imageNamed:@"genericfriendicon.png"] forState:UIControlStateNormal];
leftCelebrityLabel.text = @"No celebrities found";
return;
}
int r = arc4random() % [celebArray count];
leftCelebrity = [celebArray objectAtIndex:r];
if(leftCelebrity) {
leftCelebrityLabel.text = [NSString stringWithFormat:@"%@ %@", leftCelebrity.firstName, leftCelebrity.lastName];
[leftCelebrityButton setBackgroundImage:[[UIImage alloc] initWithData:leftCelebrity.imageData] forState:UIControlStateNormal];
} else {
NSLog(@"WouldYaViewController loadCelebrities leftCelebrity could not find any");
}
}
This code does the following:
And the Scene should look like this:
Lastly, make sure and connect up your UIComponents in the MainStoryboard.storyboard for the WouldYaViewController elements.
leftCelebrityLabel
Referencing Outlet to WouldYaViewController/leftCelebrityLabel
leftCelebrityButton
Referencing Outlet to WouldYaViewController/leftCelebrityButton
When you run the app, it loads and populates the WouldYaViewController Scene. Then, if everything is working right, it successfully retrieves a Celebrity object, randomly, and populates the UI.
(sn. You may notice, when this Scene loads, the end-user is not yet authenticated. Until the end-user attempts to affect data on the API, she may retrieve objects anonymously.)
In this tutorial, we have created our unit tests for all CRUD actions for a our very own Celebrity class we created as well as authentication without having to translate in and out of NSDictionary objects or use some other third party object models. We have used both synchronous as well as asynchronous methods within the tests to make sure they operate correctly.
Another key concept that we have demonstrated is the power of creating a complex object. In this case, an object with a blob (an image) that is seamlessly persisted and retrieved from the API. There is virtually no limit to the objects that you can exchange with your API. In fact, one could easily persist CelebrityViewController if one wanted to.
We have also built a Scene that Creates a Celebrity and a second one that Retrieves it. The CelebrityViewController Scene shows a nice pattern for authentication and the WouldYaViewController shows that you can retrieve objects from NoServer APIs anonymously which is very convenient for the developer and can greatly improve the end user experience with your application.