How to deal with MKReverseGeocoder / PBHTTPStatusC

2019-01-23 19:24发布

Since iOS 4.3 (GM Seed 10M2518) I'm getting crashes when using MKReverseGeocoder. reverseGeocoder:didFailWithError: gets called with an error like this quite often:

Error Domain=NSURLErrorDomain Code=-1011 "The operation couldn’t be completed. (NSURLErrorDomain error -1011.)" UserInfo=0x339900 {PBHTTPStatusCode=503}

The app tends to crash at these moments. This hasn't been the case in previous versions of iOS.

Any ideas what happened?

6条回答
可以哭但决不认输i
2楼-- · 2019-01-23 19:47

Google doesn't allow a single device to retrieve its location more than once within 60 sec, hence working with another method (http request instead, JSON needed) when (MKReverseGeocoder *)geocoder didFailWithError.

It works for me on 4.3.3 (3GS) and tested for 30-40 times retrieving user's location consecutively without a crash, hope it helps!

- (void) retrieveCurrentLoc {
self.geoCoder =
[[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease];
    geoCoder.delegate = self;

    [geoCoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{

NSString *fetchURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?    q=%@,%@&output=json&sensor=true", [NSString     stringWithFormat:@"%f",mapView.userLocation.location.coordinate.latitude], [NSString      stringWithFormat:@"%f",mapView.userLocation.location.coordinate.longitude]];
    NSURL *url = [NSURL URLWithString:fetchURL];
    NSString *htmlData = [NSString stringWithContentsOfURL:url];

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString:htmlData error:nil];
NSArray *placemark = [json objectForKey:@"Placemark"];
if ([[[[[placemark objectAtIndex:0]     objectForKey:@"AddressDetails"]objectForKey:@"Country"]objectForKey:@"Thoroughfare"]objectFor  Key:@"ThoroughfareName"] != NULL){
    currentAddress = [[[[[placemark objectAtIndex:0]     objectForKey:@"AddressDetails"]objectForKey:@"Country"]objectForKey:@"Thoroughfare"]objectFor  Key:@"ThoroughfareName"];}
else {
    currentAddress = [[placemark objectAtIndex:0] objectForKey:@"address"];
    }
}
查看更多
淡お忘
3楼-- · 2019-01-23 19:50

Make sure you don't release the reverse geocoder prematurely on failure:

Changing [_reverseGeocoder release] to [_reverseGeocode autorelease] in -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error fixed the problem for me.

查看更多
等我变得足够好
4楼-- · 2019-01-23 19:51

Following on from zippo77's answer, I found best results when setting the MKReverseGeocoder delegate to nil first in -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error

_reverseGeocoder.delegate = nil;
[_reverseGeocoder autorelease];
查看更多
乱世女痞
5楼-- · 2019-01-23 19:55

I had the same issue, even though my MKReverseGeocoder code followed one of the Apple examples (MKReverseGeocoder as a global autorelease variable). Also the issue appeared only on iOS 4.3 (4.0 was running no problem).

What solved the problem for me was removing the autorelease part and releasing the MKReverseGeocoder only in dealoc of the view.

查看更多
来,给爷笑一个
6楼-- · 2019-01-23 20:02

Same problem here, we checked various solutions and didn't work. The 503 response code is handled differently by the previous OS, you can easily notice that by sniffing the iPhone traffic.

Applications that rely on MKReverseGeocoder (like Gowalla) will make some pressure against Apple ... so I would expect a 4.3.1 hotfix coming these days. Or Google to change their SLA with Apple requests.

查看更多
Emotional °昔
7楼-- · 2019-01-23 20:13

Probably you are stoping location manager before geo coder will find placemark. Do it in didFindPlacemark or for error

查看更多
登录 后发表回答