I am happy with Wordpress JSON plugin, it's free. You can easily fine tune your requests to specific pages and post types, it's well documented. What you get back from your site is always a structure of dictionaries and arrays in JSON (better then XML ;). Good way to examine the JSON structure is a JSON viewer.
Some sample code to get started.
- (void)loadNewsForPage:(NSUInteger)page
{
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.mywordpresssite.com/api/get_posts/?page=%lu", (unsigned long)page]]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
if (!jsonError) {
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
// fill datastore
[self newsIntoDataStore:(NSDictionary *)jsonObject forPage:page];
}
else {
NSLog(@"returned jsonObject is not a dictionary!");
}
} else {
NSLog(@"jsonError, news: %@", jsonError);
}
}] resume];
}
- (void)newsIntoDataStore:(NSDictionary *)news forPage:(NSUInteger)page
{
if ([[news objectForKey:@"status"] isEqualToString:@"ok"]) {
self.newsPages = [[news objectForKey:@"pages"] integerValue];
NSArray *posts = [news objectForKey:@"posts"];
if (posts.count > 0) {
// store individual posts
for (NSDictionary *post in posts) {
// determine post slug
NSArray *categoriesArray = [post objectForKey:@"categories"];
NSString *postSlug = nil;
NSString *desiredSlug = @"news";
if (categoriesArray.count > 0) {
for (NSDictionary *category in categoriesArray) {
if ([[category objectForKey:@"slug"] isEqualToString:desiredSlug]) {
postSlug = desiredSlug;
}
}
}
NSString *title = [post objectForKey:@"title"];
NSDate *dateFromAPI = [self.dateFormatterFromAPI dateFromString:[post objectForKey:@"date"]];
NSString *date = [self.dateFormatterForCell stringFromDate:dateFromAPI];
NSString *imageURLStringOrg = [[[post objectForKey:@"thumbnail_images"] objectForKey:@"full"] objectForKey:@"url"];
NSString *imageURLStringConverted = [imageURLStringOrg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *attributedContentString = [[NSAttributedString alloc] initWithData:[[post objectForKey:@"excerpt"] dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
NSString *content = [attributedContentString string];
NSString *url = [post objectForKey:@"url"];
if (title && date && imageURLStringConverted && content && url) {
NSDictionary *postDictionary = @{@"title" : title, @"date" : date, @"imageURL" : imageURLStringConverted, @"content" : content, @"url" : url};
[self.newsArray addObject:postDictionary];
} else {
NSLog(@"some post item empty, skipping this post: %lu", (unsigned long)[posts indexOfObject:post]);
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
if (self.pageLoaded < self.newsPages) {
[self loadNewsForPage:self.pageLoaded + 1];
} else {
[self.delegate newsFetchingCompleted];
}
});
} else {
NSLog(@"no objects in the array");
}
} else {
NSLog(@"returned status NOT OK");
}
}