Question

I am developing an iPhone application in which I want to show nearest restaurants based on the current location For that In the applicationDidFinishLaunching I am doing this :

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]);
        }
    }

The problem scenario.

the location manager start updating the location

The webservice gets executed before that so I can't get the location values .

I I am putting the web service call in the delegate method then the application launches before the web service gets executed. in the delegate method I am setting the latitude and longitude in the appropriate strings.

The Problem is that how ensure that the service will not be called until the location manager updates that location and then pass the location to the web service and then call that web service ..

Was it helpful?

Solution

CLLocationManaged has the delegate method.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

it will be called when CLLocationManager will receive some coordinates. Or

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

it will be called when locationManager cannot receive any coordinates. Or when user don't allow to find current location. Also you should call stopUpdatingLocation if you don't want to receive more accurate results.

So you can make your request inside delegate method to be sure is will be called only after updating current location.

OTHER TIPS

One thing I'd recommend is to do your urlRequest in a thread instead as hinted Sixten Otto.

I would setup the locationManager in viewDidLoad or viewDidAppear like you have. Then there are two ways I would call the urlrequest:

  1. Have the request be performed in didUpdateToLocation delegate, then you will know that your urlrequest will only execute after the location is found. This is the suggestion that Morion made.

  2. The other way is to have a notification in the didUpdateToLocation delegate so that when the location is found, it notifies a function to perform the urlRequest.Look up this Tutorial for setting up notifications, it is very helpful and good to know for a lot of programs.

Also, as I mentioned before, you should perform an urlrequest in a thread. The reason is, your request could be locked up and your user would have no control to exit the request. To do this, Setup an NSOperation and place it in the NSOperationQueue:

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget: self 
                                                                                selector: @selector(METHOD:) 
                                                                                  object: OBJECTorNIL;
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:operation];

for more threading info, look up this Tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top