Я хочу сделать HTTP-запрос, а часть запроса содержит lat и lng.Использование делегатов в моделях для передачи информации между ними
Для этого у меня есть мода. Один из его делегатов позволяет мне знать, когда у него есть место. Когда это произойдет, я вызываю свой пользовательский делегат, который реализуется в моей модели Foursquare.
Я запускаю все в своем MainViewController, создавая экземпляр модели местоположения и вызываю метод startUpdatingLocation
. Реализация выглядит примерно так:
Расположение Модель:
- (void)startLocationManager
{
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Error:%@", error.userInfo);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if (locations) {
_location = [locations lastObject];
[self.delegate location:self didFinishFindingLocation:_location];
}
}
Foursquare модель:
- (void)location:(Location *)loc didFinishFindingLocation:(CLLocation *)location
{
NSString *search = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%f,%f&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=%@&client_secret=%@&v=%@", location.coordinate.latitude, location.coordinate.longitude, kFourdquareAPIKey, fFoursquareSecret, [Foursquare getCurrentDate]];
NSURL *url = [NSURL URLWithString:search];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dataDictionary = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
self.dataResponse = dataDictionary[@"response"][@"venues"];
NSLog(@"%@", self.dataResponse);
}];
[task resume];
}
Наконец, я делаю это в моем MainViewController:
Location *location = [[Location alloc] init];
[location startLocationManager];
Ничего не регистрируется на консоль, и симулятор не просит у меня разрешения на получение мест. Ничего.
1) Неправильно ли использовать таких делегатов? 2) Есть ли лучший способ?
Учитывая, что ответ 1 нет, что случилось с моим кодом?
Вы получаете звонок в - (void) местоположение: (Место *) loc didFinishFindingLocation: (CLLocation *) место. Я сомневаюсь, что вы устанавливаете делегат для своей модели Foursquare. – Pawan