views:

322

answers:

1

Hey guys,

I'm having a problem with doing a sendSynchronousRequest failing. It only fails after I try to get the current geolocation and the user hits "Don't Allow". And it only happens under 3.1.2. (As far as I can tell. It works fine in 3.0.1.)

Here's what I do:

I set up a very basic test app, that has almost nothing in it. In applicationDidFinishLaunching I add a call to my function, test, which is here:

- (void) test
{
 CLLocationManager *mLM;

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

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

My delegate methods are pretty simple too:

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

Finally, here's my 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];
}

The call to sendSynchronousRequest hangs. This has been very frustrating. Does anyone have any ideas?

Thanks for any help,

henning

+1  A: 

This will work it's magic. Make sure the mLm is your class varialble, so you can to this:

  • (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!!! }

Shankar Nathan
Thanks for that - it almost fixed it, but not quite. The sendRequest succeeded, but I got a fail later on. But I played around a bit, and found a small variation of your code that works: [mLM stopUpdatingLocation]; mLM.delegate = nil; [mLM release]; mLM = [[CLLocationManager alloc] init]; mLM.delegate = self;