Iphone sendSynchronousRequest在第3.1.2节“之后失败”;Don';“不允许”;位置提取
我在执行Iphone sendSynchronousRequest在第3.1.2节“之后失败”;Don';“不允许”;位置提取,iphone,objective-c,cocoa-touch,core-location,Iphone,Objective C,Cocoa Touch,Core Location,我在执行sendSynchronousRequest失败时遇到问题。只有在我尝试获取当前地理位置并且用户点击“不允许”后,它才会失败。它只发生在3.1.2下。(据我所知,它在3.0.1中运行良好。) 我是这样做的: 我建立了一个非常基本的测试应用程序,里面几乎什么都没有。在applicationdFinishLaunching中,我添加了对函数test的调用,该函数位于: - (void) test { CLLocationManager *mLM; mLM = [[CLLocationM
sendSynchronousRequest
失败时遇到问题。只有在我尝试获取当前地理位置并且用户点击“不允许”后,它才会失败。它只发生在3.1.2下。(据我所知,它在3.0.1中运行良好。)
我是这样做的:
我建立了一个非常基本的测试应用程序,里面几乎什么都没有。在applicationdFinishLaunching
中,我添加了对函数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
}
最后,这是我的请求:
- (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
的调用挂起。这是非常令人沮丧的。有人有什么想法吗?这会奏效的,真神奇。确保传销是您的类变量,因此您可以:
- (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!!!
}
这会起作用的,这很神奇。确保传销是您的类变量,因此您可以:
- (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!!!
}
您正在获取的URL是否存在网络问题?调用是否无限期挂起?如果用户允许地理位置获取,sendSynchronousRequest会成功。除3.1.2中用户说“不允许”地理定位获取外,它在其他任何地方都可以正常工作。它似乎无限期地挂起,尽管我没有等待超过几分钟来发现。作为一个数据点,尝试在自己的线程中运行sendRequest(只使用NSInvocationOperation)。由于不知道如何使用NSInvocationOperation,我尝试使用performSelectorInBackground,它似乎可以工作。虽然这对我帮助不大,因为我仍然想解决原来的问题。不过,值得注意的是。我无意中对那句话投了赞成票。在通过和失败的情况下,能否从sendRequest发布堆栈跟踪?我的想法是,当您的委托方法被调用时,应用程序可能处于某种模式运行循环中,从而使网络连接无法工作。您获取的URL是否可能存在网络问题?调用是否无限期挂起?如果用户允许地理位置获取,sendSynchronousRequest会成功。除3.1.2中用户说“不允许”地理定位获取外,它在其他任何地方都可以正常工作。它似乎无限期地挂起,尽管我没有等待超过几分钟来发现。作为一个数据点,尝试在自己的线程中运行sendRequest(只使用NSInvocationOperation)。由于不知道如何使用NSInvocationOperation,我尝试使用performSelectorInBackground,它似乎可以工作。虽然这对我帮助不大,因为我仍然想解决原来的问题。不过,值得注意的是。我无意中对那句话投了赞成票。在通过和失败的情况下,能否从sendRequest发布堆栈跟踪?我的想法是,当您的委托方法被调用时,应用程序可能处于某种模式的运行循环中,从而使网络连接无法工作。为此,我表示感谢——它几乎修复了它,但还没有完全修复。sendRequest成功了,但我后来失败了。但我仔细研究了一下,发现您的代码有一个小的变化,可以正常工作:[mLM StopUpdategLocation];传销代表=无;[传销释放];mLM=[[CLLocationManager alloc]init];mLM.delegate=self;谢谢你,它几乎把它修好了,但还没修好。sendRequest成功了,但我后来失败了。但我仔细研究了一下,发现您的代码有一个小的变化,可以正常工作:[mLM StopUpdategLocation];传销代表=无;[传销释放];mLM=[[CLLocationManager alloc]init];mLM.delegate=self;