【发布时间】:2014-12-27 09:29:02
【问题描述】:
我需要从我创建的博客中获取信息并将其显示在 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
当我运行这个应用程序时出现错误。如果你能帮我解决这个问题会很有帮助。
【问题讨论】:
-
这是我得到的错误搜索结果:{“error”:{“errors”:[{“domain”:“usageLimits”,“reason”:“accessNotConfigured”,“message” :“未配置访问。您的项目未启用 API,或者您的 API 密钥上配置了 per-IP 或 per-Referer 限制,并且请求与这些限制不匹配。请使用 Google Developers Console 更新您的配置。", "extendedHelp": "console.developers.google.com" } ],
-
这是延续 "code": 403, "message": "Access Not Configured. API 未为您的项目启用,或者配置了 per-IP 或 per-Referer 限制您的 API 密钥和请求不符合这些限制。请使用 Google Developers Console 更新您的配置。” } }