How to validate a physical address in iOS

373 views Asked by At

Using this code:

- (void)geocodeAddress:(NSString *)address state:(NSString *)state zip:(NSString *)zip completion:(ResultAndErrorHandler)completion {

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:[NSString stringWithFormat:@"%@, %@, %@, United States", address, state, zip] completionHandler:^(NSArray* placemarks, NSError* error){

        if (error) {
            completion(nil, error);
            return;
        }

        CLPlacemark *placemark = [placemarks firstObject];
        completion(placemark, nil);

    }];
}

The problem is that it never throws an error. No matter what I enter for "address", it just picks a random location within the zip code.

For example if I enter "x z" for address "CA" for state and "90210" for zip it just picks the closest match for the address "x z" in this zip code.

But "x z" is clearly an invalid address. How can I detect that this is not a real address and that the geocoder is just guessing?

0

There are 0 answers