【发布时间】:2009-10-28 05:42:04
【问题描述】:
我正在开发一个 iPhone 应用程序,我想在其中根据当前位置显示最近的餐馆 为此,在 applicationDidFinishLaunching 我正在这样做:
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.0.150/server1/Service.asmx/nearest?lat1=23.013163&lon1=72.559068"];
NSMutableURLRequest* request2=[NSMutableURLRequest requestWithURL:url];
[request2 setHTTPMethod:@"GET"];
[request2 setTimeoutInterval:10];
NSURLResponse *response=nil;
NSError *err=nil;
NSData *data1=[[NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err] retain];
if(data1 == nil)
{
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data1];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
@try {
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
}
@catch (NSException * e) {
NSLog(@"Exception in parsing %@ %@",[e name], [e reason]);
}
}
问题场景。
位置管理器开始更新位置
Web 服务在此之前执行,因此我无法获取位置值。
我将 Web 服务调用放在委托方法中,然后应用程序在 Web 服务执行之前启动。 在委托方法中,我在适当的字符串中设置纬度和经度。
问题是如何确保在位置管理器更新该位置并将位置传递给 Web 服务然后调用该 Web 服务之前不会调用该服务..
【问题讨论】:
-
我能给出的最重要的建议是:不要像这样在你的应用委托中做所有这些,也不要试图强迫你的应用与网络同步交互。它可能是不那么复杂的代码,但它是一个更糟糕的用户体验。默认情况下,核心位置和 URL 加载系统都是异步的。拥抱它!
-
我不明白你的想法。你能详细说明一下吗?
标签: iphone core-location