在“不允许”位置获取后,sendSynchronousRequest在3.1.2下失败

时间:2009-11-03 19:06:35

标签: iphone objective-c cocoa-touch core-location

我遇到了sendSynchronousRequest失败的问题。它只在我尝试获取当前地理位置并且用户点击“不允许”后失败。它只发生在3.1.2下。 (据我所知。它在3.0.1中工作正常。)

这就是我的所作所为:

我设置了一个非常基本的测试应用程序,其中几乎没有任何内容。在applicationDidFinishLaunching中,我添加了对我的函数test的调用,其中包括:

- (void) test
{
 CLLocationManager *mLM;

 mLM = [[CLLocationManager alloc] init];
 mLM.delegate = self;

 if ( [mLM locationServicesEnabled] )
 {
  [mLM startUpdatingLocation];
 }
}

我的委托方法也很简单:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 [manager stopUpdatingLocation];
 [self sendRequest]; // succeeds
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
 [manager stopUpdatingLocation];
 [self sendRequest]; // fails
}

最后,这是我的sendRequest:

- (void) sendRequest
{
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
 [request setURL:[NSURL URLWithString:@"https://theurl"]];  // this is actually a valid URL, changed here for privacy
 [request setHTTPMethod:@"GET"];
 [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

 NSString    *unpw   = @"username:password";
 NSString    *base64 = [NSString base64StringFromString:unpw];
 [request addValue:[NSString stringWithFormat:@"Basic %@", base64] forHTTPHeaderField:@"Authorization"];

 NSURLResponse *response = nil;
 NSError    *error = nil;
 NSData    *respdata = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

 [request release];
}

sendSynchronousRequest的调用挂起。这非常令人沮丧。有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

这将起到魔力。确保mLm是你的类变量,所以你可以这样:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
 [self.mLm release];
 self.mLM = [[CLLocationManager alloc] init];
 self.mLM.delegate = nil;

 [self sendRequest]; // fails - This time it will work!!!
}