我在執行sendSynchronousRequest
失敗時遇到問題。在我嘗試獲取當前地理位置並且用戶點擊「不允許」之後,它只會失敗。它只發生在3.1.2下。 (據我可以告訴它在3.0.1工作正常。)在「不允許」位置獲取後,3.1.2的sendSynchronousRequest失敗
這是我做的:
我建立了一個非常基本的測試程序,已經在它幾乎沒有。在applicationDidFinishLaunching
我打電話添加到我的功能測試,這是在這裏:
- (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
掛起。這非常令人沮喪。有沒有人有任何想法?
你的網址可能會有網絡故障嗎?通話是否無限期掛起? – 2009-11-03 19:12:05
如果用戶允許地理位置提取,sendSynchronousRequest成功就可以。它在其他地方總能正常工作,除非用戶在3.1.2中說「不允許」到地理位置獲取之後。 它似乎無限期地掛起,雖然我沒有等待超過幾分鐘才能找到。 – Henning 2009-11-03 19:22:06
作爲一個數據點,嘗試在自己的線程中運行sendRequest(只需使用NSInvocationOperation)。 – nall 2009-11-03 19:50:07