Search code examples
iosblock

How To return a value From block in iOS


Im using google geocode Svc to get Lat and Lng from the Address,Some times Google geocode Svc is getting failed(Because of toomany counts per second or perday ), So i want to use Default Geocoder Svc provided by Apple.

See here My code

 -(void)getLatandlongfromAddress
   {
    CLLocationCoordinate2D Coordinate=[self   geoCodeUsingAddress:@"orlando"];

   }

- (CLLocationCoordinate2D)geoCodeUsingAddress:(NSString *)address
 {
   NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];
        NSError *err = nil;
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
        if (!err)
        {
            NSLog(@"status = %@",[jsonDict objectForKey:@"status"]);
            if ([[jsonDict objectForKey:@"status"] isEqualToString:@"OK"])
            {

                  //If status is cmng Im returned lat and long  


            }
            else
           {
                 // I want to use geocode Svc


                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Orlando 32835" completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else {

        NSLog(@"%@",placemarks);
        CLPlacemark *placemark = [placemarks lastObject];

    }
}];




           }

         }


}

Guide me with any idea, Thanks..


Solution

  • There are several errors in your code:

    1. If you call [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err]; and pass kNilOptions as the options parameter you don't get back a mutable dictionary but an immutable one.
    2. You should not check if there is no error but instead if your data is not nil.

    Here is your corrected code (including the completion handler):

    - (CLLocationCoordinate2D)geoCodeUsingAddress:(NSString *)address completionHandler:(void (^)(CLLocation *location, NSError *error))completionHandler {
       NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];    // Whatever `req` is...
        NSError *jsonError = nil;
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
        if (jsonDict) {
            NSLog(@"status = %@", [jsonDict objectForKey:@"status"]);
            if ([[jsonDict objectForKey:@"status"] isEqualToString:@"OK"]) {
                // If status is cmng Im returned lat and long
                // Get them from you local file
                CLLocationDegrees latitude = 30.0;
                CLLocationDegrees longitude = 50.0;
                CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
                completionHandler(location, nil);
            } else {
                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                [geocoder geocodeAddressString:@"Orlando 32835" completionHandler:^(NSArray *placemarks, NSError *error) {
                    if (placemarks.count) {
                        NSLog(@"%@",placemarks);
                        CLPlacemark *placemark = [placemarks lastObject];
                        completionHandler(placemark.location, nil);
                    } else {
                        NSLog(@"%@", error);
                        completionHandler(nil, error);
                    }
                }];
            }
        } else {
            // Populate the error
            completionHandler(nil, [NSError errorWithDomain:@"YOUR_DOMAIN" code:1000 userInfo:nil]);
        }
    }
    

    You call it like any other method with a completion handler:

    [self geoCodeUsingAddress:@"Orlando 32835" completionHandler:^(CLLocation *location, NSError *error) {
        if (location) {
            // Use location
        } else {
            NSLog(@"Error: %@", error.userInfo);
        }
    }];