KAR
KAR

Reputation: 3361

AFNetworking with CLLocation gives error

I am working with AFNetworking 3.0 and CLLocation for weather app.

When I click on button, API must have to update with location. But It gives me error after updating location.

I am using world weather api for getting weather details.

The error is,

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (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 } { status code: 403, headers { "Access-Control-Allow-Origin" = "*"; Age = 0; "Cache-Control" = "public, max-age=120"; Connection = "keep-alive"; "Content-Encoding" = 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=Request failed: forbidden (403)}

I have refer all the articles on net but cannot find the proper answer for this.

I have some methods for perform this functionality,

- (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);
}

This methods call when I tapped button named API. And updating locations and give the data in tableview. But its not working.

Button method is :

- (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");
}

I am refering this tutorial for AFNetworking demo,

https://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

As per that tutorial everything is working fine for them in AFNetworking 2.0 but not form me.

So what is the issue in my code?

Upvotes: 0

Views: 134

Answers (2)

KAR
KAR

Reputation: 3361

I have solved the problem.

The problem is with access. I have account in world weather with free user. and the api url is ,

http://api.worldweatheronline.com/free/v1/

So with free acount they not providing some access to their api.

I changed the url to,

http://api.worldweatheronline.com/premium/v1/

So now they are allowing to access the api and the error is now gone.

Upvotes: 1

iSashok
iSashok

Reputation: 2446

Your client received HTTP Error 403 Forbidden that means

The request was a valid request, but the server is refusing to respond to it. The user might be logged in but does not have the necessary permissions for the resource.

So check you authorization status. For more info please look the link https://en.wikipedia.org/wiki/HTTP_403

Upvotes: 1

Related Questions