Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

teamtreehouse blog API

Hi. I'm working through the Data Modeling section of Build a Blog Reader iPhone App. I'm experimenting with the data model to download blog posts from the TeamTreehouse blog feed by altering the URL. For example,

http://blog.teamtreehouse.com/api/get_recent_summary/?page=2

http://blog.teamtreehouse.com/api/get_recent_summary/?count=20

When I use these URLs in a web browser, I get the expected results (ie. a different page of blog posts, or a different number of blog posts).

When I use these URLs in my app, I keep getting the same (undesired) result of always getting the 12 most recent blog posts in the feed. In the app, it doesn't seem to respect the "count" or "page" part of the URL.

The code:

static NSString* const BlogAddress = @"http://blog.teamtreehouse.com/api/get_recent_summary/";


NSMutableArray* LoadBlogData(){

    NSData* jsonData = nil;
    NSError* deserializationError = nil;
    NSString* pagedAddress = @"";
    NSMutableArray *result = [NSMutableArray array];


    int const targetPostCount = 20;

    for (int i=1; true; i++){
        pagedAddress = [NSString stringWithFormat:@"%@?page=%d", BlogAddress, i];

        NSLog(@"\n\n\n%@", pagedAddress);

        jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pagedAddress]];

        if (jsonData == nil)
            break;

        NSDictionary* pageDataDictionary = [NSJSONSerialization
                                            JSONObjectWithData:jsonData
                                            options:0
                                            error:&deserializationError];

        if (deserializationError || pageDataDictionary == nil)
            break;


        NSArray* blogPostsDownloaded = pageDataDictionary[@"posts"];


        for (NSDictionary* blogPostDownloaded in blogPostsDownloaded) {
            BlogPost* bp = [BlogPost blogPostWithDictionary:blogPostDownloaded];
            [result addObject:bp];

            NSLog(@"%@", bp);

            if (result.count >= targetPostCount)
                break;
        }

        if (result.count >= targetPostCount)
            break;

    }

    return result;
}

jsonData keeps getting the first page of 12 posts. I'm expecting it to have the i-th page of 12 posts.

Questions:

  1. What's the right way for my app to request the i-th page of blog post summaries?

  2. What's the right way to request n blog post summaries rather than just the default 12?

  3. Is the server somehow differentiating between browsers and apps? Can I force/trick it into thinking my app is a browser? (Also, should I? ;-)

  4. Is there documentation for the teamtreehouse.com web API?

Cheers.

I edited the question to contain the full method where I load the blog data.

In the NSLog output, the values of pagedAddress are:

http://blog.teamtreehouse.com/api/get_recent_summary/?page=1

http://blog.teamtreehouse.com/api/get_recent_summary/?page=2

The loop exits when I have 20 blog summaries so it only ever iterates twice.

Thanks