2009-11-03 54 views
1

我在執行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掛起。這非常令人沮喪。有沒有人有任何想法?

+0

你的網址可能會有網絡故障嗎?通話是否無限期掛起? – 2009-11-03 19:12:05

+0

如果用戶允許地理位置提取,sendSynchronousRequest成功就可以。它在其他地方總能正常工作,除非用戶在3.1.2中說「不允許」到地理位置獲取之後。 它似乎無限期地掛起,雖然我沒有等待超過幾分鐘才能找到。 – Henning 2009-11-03 19:22:06

+0

作爲一個數據點,嘗試在自己的線程中運行sendRequest(只需使用NSInvocationOperation)。 – nall 2009-11-03 19:50:07

回答

1

這將工作它是神奇的。確保mLm是您的類別varialble,所以你可以這樣:

- (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!!! 
} 
+0

謝謝你 - 它幾乎解決了它,但不完全。 sendRequest成功了,但之後我失敗了。但我玩了一下,發現你的代碼的一小部分變化: \t \t [mLM stopUpdatingLocation]; \t mLM.delegate = nil; \t [mLM release]; \t mLM = [[CLLocationManager alloc] init]; \t mLM.delegate = self; – Henning 2009-11-04 14:08:24

相關問題