如何从谷歌博客中获取信息以显示在IOS应用程序上

时间:2014-12-27 09:29:02

标签: ios blogger

我需要从我创建的博客中获取信息并在IOS应用程序上显示。这是我到目前为止所做的代码

#import "EventsTableViewController.h"
#import "Events.h"

@interface EventsTableViewController ()

@end

@implementation EventsTableViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.eventsArray = [NSMutableArray array];
self.eventsDictionary = [NSMutableDictionary dictionary];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)viewWillAppear:(BOOL)animated{
[self searchForEvents];
}

- (void)searchForEvents {
NSString *eventSearchURLString = [NSString stringWithFormat:@"https://www.googleapis.com/blogger/v3/blogs/1562818803553764290?key=AIzaSyBTOxz-vPHgzIkw9k88hDKd99ILTaXTt0Y"];
NSURL *eventSearchURL = [NSURL URLWithString:eventSearchURLString];
NSURLRequest *eventSearchURLRequest = [NSURLRequest requestWithURL:eventSearchURL];

NSURLSession *sharedURLSession = [NSURLSession sharedSession];
NSURLSessionDataTask *searchEventsTask = [sharedURLSession dataTaskWithRequest:eventSearchURLRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if (error) {
            UIAlertView *searchAlertView = [[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
            [searchAlertView show];
        }
        else{
            NSString *resultString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Search Results: %@", resultString);
            NSError *jsonParseError = nil;
            NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParseError];
            if (jsonParseError) {
                UIAlertView *jsonParseErrorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:jsonParseError.localizedDescription delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
                [jsonParseErrorAlert show];
            }
            else {
                for (NSString *key in jsonDictionary.keyEnumerator) {
                    NSLog(@"First level key : %@", key);

                    [self.eventsArray removeAllObjects];
                    [self.eventsDictionary removeAllObjects];

                    NSArray *searchResultsArray = [jsonDictionary objectForKey:@"results"];
                    for (NSDictionary *eventInfoDictionary in searchResultsArray) {
                        Events *event = [[Events alloc] init];
                        event.eventName = [eventInfoDictionary objectForKey:@"title"];
                        event.eventDescription = [eventInfoDictionary objectForKey:@"content"];

                        NSString *eventFirstLetter = [event.eventName substringToIndex:1];
                        NSMutableArray *eventsWithFirstLetter = [self.eventsDictionary objectForKey:eventFirstLetter];

                        if (!eventsWithFirstLetter) {
                            eventsWithFirstLetter = [NSMutableArray array];
                            [self.eventsArray addObject:eventFirstLetter];

                        }

                        [eventsWithFirstLetter addObject:event];
                        [self.eventsDictionary setObject:eventsWithFirstLetter forKey:eventFirstLetter];

                    }

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

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[searchEventsTask resume];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.eventsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.eventsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"eventCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

NSString *firstLetter = [self.eventsArray objectAtIndex:indexPath.row];
NSArray *eventsWithFirstLetter  = [self.eventsDictionary objectForKey:firstLetter];
Events *event = [eventsWithFirstLetter objectAtIndex:indexPath.row];
cell.textLabel.text = event.eventName;

return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.eventsArray objectAtIndex:section];
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.eventsArray;
}

- (int)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title             atIndex:(NSInteger)index {
return [self.eventsArray indexOfObject:title];
}


@end

当我运行此应用程序时出现错误。如果您可以帮我解决问题,将会有所帮助。

1 个答案:

答案 0 :(得分:0)

你正在降低选票,因为这是一个很容易通过搜索自己解决的问题。

但是为了帮助您,您正在寻找的是访问Blogger内容的“API”。

Blogger API的here is a Getting Started guide