I am building an iphone app the uses AFNetworking to GET and POST data to a rails-backed server. Everything is working fine, but now I am trying to pass lat/lng parameters so that I load nearby posts. I am pretty sure I set it up correctly- the heroku logs show a get request made with lat/lng- but the problem I am having is a more basic one. In my indexViewController, in the viewDidLoad method I try to call
[self loadPosts]
But I get into trouble because, below that, when I define loadPosts and call the fetchNearbyPosts method, I need to call a CLLocation as part of its implementation- which causes an undeclared identifier error. I can fix that by declaring:
loadPosts:(CLLocation *)location {
But then, in viewDidLoad, when I change [self loadPosts:(CLLocation *)location]; that line now gets an undeclared identifier error for 'location'. So I am going in circles.
My fetchNearbyPosts method is like so:
+ (void)fetchNearbyPosts:(CLLocation *)location
withBlock:(void (^)(NSArray *posts, NSError *error))completionBlock
{
NSDictionary *parameters = @{
@"lat": @(location.coordinate.latitude),
@"lng": @(location.coordinate.longitude)
};
[[APIClient sharedClient] getPath:@"/posts" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (operation.response.statusCode ==200) {
NSArray *posts = [Post postsWithJSON:responseObject];
completionBlock(posts, nil);
} else {
NSLog(@"Recieved an HTTP %d: %@", operation.response.statusCode, responseObject);
completionBlock(nil, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock(nil, error);
NSLog(@"Error: %@", error);
}];
}
And my full loadPosts method is:
- (void)loadPosts:(CLLocation *)location {
[Post fetchNearbyPosts:location withBlock:^(NSArray *posts, NSError *error) {
if (posts) {
NSLog(@"Recieved %d posts", posts.count);
self.posts = [NSMutableArray arrayWithArray:posts];
[self.tableView reloadData];
[self.tableView setNeedsLayout];
} else {
[[[UIAlertView alloc] initWithTitle:@"ERROR"
message:@"Couldn't fetch the posts."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}];
}
I think this is pretty basic- but how does one go about calling loadPosts in ViewDidLoad with a CLLocation? Any help would be greatly appreciated. Thanks
As far as I understand you need to get your current location to pass it as a parametr for your - (void)loadPosts:(CLLocation *)location method, which will be called in your viewDidLoad
add this method to your view controller:
-(CLLocation) getLocation{
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation * location = [locationManager location];
return location;
}
and then, in your viewDidLoad call this
[self loadPosts: [self getLocation]];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.