ios - AFNetworking 与 CLLocation 给出错误

标签 ios objective-c cllocationmanager cllocation afnetworking-3

我正在为天气应用程序使用 AFNetworking 3.0 和 CLLocation。

当我点击按钮时,API 必须更新位置信息。但它在更新位置后给我错误。

我正在使用 world weather api 获取天气详细信息。

错误是,

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

我引用了网上的所有文章,但找不到合适的答案。

我有一些方法可以执行此功能,

- (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 的按钮时调用此方法。并更新位置并在表格 View 中提供数据。但它不起作用。

按钮方法是:

- (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 中一切正常,但不适用于我。

那么我的代码有什么问题呢?

最佳答案

您的客户端收到 HTTP Error 403 Forbidden 这意味着

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.

所以检查你的授权状态。有关更多信息,请查看链接 https://en.wikipedia.org/wiki/HTTP_403

关于ios - AFNetworking 与 CLLocation 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39566496/

相关文章:

javascript - Jquery .text() 在带有 cordova 的 ios 上无法正常工作

ios - 阻止 SKSpriteNode 离开 View (Swift)

ios - 在 Swift 的 MKMapView 中显示当前位置和更新位置

iphone - CLLocationManager 的 distanceFilter 属性无法正常工作

android - 使用 iOS 分布式应用程序时,Google Cloud Messaging 显示 "notRegistered"

ios - 内部带有 UIWebview 的滞后 ScrollView

c++ - 未找到符号,应在平面命名空间 ObjC++ 中

IOS:didSelectRowAtIndexPath 在选择单个单元格时选择多个单元格

objective-c - 旋转 CGImage

objective-c - iMac 的位置管理器