Fat Fractal Tutorial for iOS: Getting Started

Learn how to make an iOS app with a back end like Twitter in this beginner Fat Fractal tutorial! By .

Leave a rating/review
Save for later
Share
You are currently viewing page 5 of 5 of this article. Click here to view the first page.

Retrieving And Displaying The First Tweet

Open MainFeedViewController.m, import your Micropost.h file:

#import "Micropost.h"

Next, you need an array to hold all the posts that you download from the web service, and a property to keep track of your current user. Change the @interface directive inside MainFeedViewController.m to look like this:

@interface MainFeedViewController ()
@property (strong, nonatomic) NSMutableArray *postsArray;
@property (strong, nonatomic) FFUser *currentUser;
@end

Then add this new method:

- (void)refresh {
    if ([[FatFractal main] loggedInUser]) {
        self.currentUser = [[FatFractal main] loggedInUser];
        [self refreshTableAndLoadData];
    }
}

Basically, what you are doing here is to check if there is a logged in user. If there is, you will assign the currentUser to the currently logged in user, as well as refresh the table with the new data from your backend.

Call this method from both viewDidLoad and userIsAuthenticatedFromAppDelegateOnLaunch:

[self refresh];

Let’s implement the refreshTableAndLoadData method now. Add the method below your viewDidLoad method:

-(void)refreshTableAndLoadData {

    // Clean the array
    if (_postsArray) {
        [_postsArray removeAllObjects];
        _postsArray = nil;
    }

    // (Re) Create the array
    _postsArray = [NSMutableArray array];

    [[FatFractal main] getArrayFromUri:@"/Micropost" onComplete:^(NSError *theErr, id theObj, 	 	NSHTTPURLResponse *theResponse) {

		if (theObj) {
            _postsArray = (NSMutableArray *)theObj;

            [self.tableView reloadData];
        }
    }];
}

You will check if the postsArray instance variable exists. If so, you will remove all objects from it and set it to nil. You then create the postsArray again.

To retrieve data from your FatFractal backend, you just need to call getArrayFromUri: on your singleton, and pass in the URI endpoint for the data you want to retrieve. In this case, it is /Micropost. This will retrieve all the objects from https://.fatfractal.com//ff/resources/Micropost for us.

Remember to call the refreshTableAndLoadData method in your callback too, so that this method will be invoked when you log in from the App Delegate.

#pragma mark - Public Methods
-(void)userIsAuthenticatedFromAppDelegateOnLaunch {
    [self refreshTableAndLoadData];
}

Lastly, you need to set up your table view to be able to show the data. Change your table view data source methods to look like this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_postsArray count];
}

And add this code inside your tableView:cellForRowAtIndexPath: after the “To Do: Display Microposts from posts array” comment:

if ([_postsArray count] > 0) {
    Micropost *micropost = [_postsArray objectAtIndex:indexPath.row];
    NSString *text = micropost.content;
    CGSize constraint = CGSizeMake(TEXT_WIDTH, 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] 						constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    if (!micropostLabel) {
        micropostLabel = (UILabel *)[cell viewWithTag:1];
    }
    [micropostLabel setText:text];
    [micropostLabel setFrame:CGRectMake(TEXTOFFSET_X, TEXTOFFSET_Y, TEXT_WIDTH, MAX(size.height + 										5, 60.0f))];

    if (!fullnameLabel) {
        fullnameLabel = (UILabel *)[cell viewWithTag:3];
    }
    [fullnameLabel setText:micropost.user.firstName];

    if (!profileImageView) {
        profileImageView = (UIImageView *)[cell viewWithTag:2];
    }
    profileImageView.frame = CGRectMake(CELL_CONTENT_MARGIN, (MAX(size.height + 35, 60.0f) - 								IMAGE_SIDE)/ 2, IMAGE_SIDE, IMAGE_SIDE);

    [micropostLabel sizeToFit];
}

Allright – it’s to see the fruits of your labour! Build and run the app, and you should see the tweet that you posted earlier appear:

FirstTweetAppearingInTable

Where To Go From Here?

Here is the complete example project from the above Fat Fractal tutorial.

In this part, you learnt how to set up the Fat Fractal backend, use the Fat Fractal iOS SDK, how to build your own authentication system, post data to the backend, and retrieve this data from the backend.

But there’s a lot more to learn with Fat Fractal! I could show you how to extend this app even further to allow users to follow each other, upload images, and more – demonstrating some neat functionality about Fat Fractal along the way.

If you would like to see more about Fat Fractal, please let me know! Otherwise, if you have any questions or comments, please join the forum discussion below.