我与AFNetworking 3.0和CLLocation工作的天气应用程序。AFNetworking与CLLocation给出错误
当我点击按钮,API必须与位置更新。但是它在更新位置后给我错误。
我使用世界天气 API来获取天气信息。
的误差是,
错误域= com.alamofire.error.serialization.response代码= -1011 “请求失败:禁止(403)” 的UserInfo = {com.alamofire。 serialization.response.error.response = {URL: http://api.worldweatheronline.com/free/v1/weather.ashx?format=json&key=1b8c4f157aec499cba0120254161609&num_of_days=5&q=19.017615%2C72.856164} {状态码:403,标头{ “访问控制允许来源”= “*”; 年龄= 0; “Cache-Control”=“public,max-age = 120”; Connection =“keep-alive”; “内容编码”= gzip; “Content-Length”= 96; “Content-Type”=“application/json; charset = utf-8”; Date =“Mon,19 Sep 2016 05:53:12 GMT”; Expires =“Mon,19 Sep 2016 05:55:13 GMT”; Vary =“Accept-Encoding”; Via = WebCelerate; “X-Cache”= MISS; “X-Powered-By”=“ASP.NET”; “X-Webcelerate”=“WebCelerate - www.ukfast.co.uk/web-acceleration.html”; “X-node”=“zfdl_api_01”; “access-control-allow-headers”=“content-type”; }},NSErrorFailingURLKey = http://api.worldweatheronline.com/free/v1/weather.ashx?format=json&key=1b8c4f157aec499cba0120254161609&num_of_days=5&q=19.017615%2C72.856164, com.alamofire.serialization.response.error.data = < 7b202264 61746122 3a207b20 22657272 6f72223a 205b207b 226d7367 223a2022 41504920 6b657920 646f6573 206e6f74 20686176 65206163 63657373 20746f20 74686520 7265736f 75726365 2e22207d 205d207d 7D>, NSLocalizedDescription =请求失败:禁止(403)}
我已经参考了网上的所有文章,但无法找到正确的答案。
我有执行该功能的一些方法,
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{NSLog(@"location updated");
// Last object contains the most recent location
CLLocation *newLocation = [locations lastObject];
// If the location is more than 5 minutes old, ignore it
if([newLocation.timestamp timeIntervalSinceNow] > 300)
return;
[self.locationManager stopUpdatingLocation];
WeatherHTTPClient *client = [WeatherHTTPClient sharedWeatherHTTPClient];
client.delegate = self;
[client updateWeatherAtLocation:newLocation forNumberOfDays:5];
}
- (void)weatherHTTPClient:(WeatherHTTPClient *)client didUpdateWithWeather:(id)weather
{
self.weather = weather;
self.title = @"API Updated";
[self.tableView reloadData];
NSLog(@"API updated");
}
- (void)weatherHTTPClient:(WeatherHTTPClient *)client didFailWithError:(NSError *)error
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
NSLog(@"error - %@", error);
}
这个方法调用时,我拍了拍按钮命名的API。并更新位置并在tableview中提供数据。但它不工作。
按钮的方法是:
- (IBAction)apiTapped:(id)sender
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
NSLog(@"apiTapped");
}
我指本教程AFNetworking演示,
https://www.raywenderlich.com/59255/afnetworking-2-0-tutorial
按照该教程一切正常为他们AFNetworking 2.0,但没有形成我。
那么,什么是我的代码的问题?
如果您的服务器是SSL认证请使用https而不是http。 –