Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofill city and state from zip code in iOS

I have three textfields in my view. 1. Zip code, 2. City and 3. State.

How to autofill city and state field from the zip code in iOS?

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    int length = [currentString length];
    if(length > 5)
    {
        return NO;
    }
    if(length == 5)
    {
        [self getCityAndState];
    }
    return YES;
}

- (void) getCityAndState
{
    //How to use google (or any) api to autofill city and state in objective - c?
}
like image 512
Deepak Thakur Avatar asked Apr 30 '14 11:04

Deepak Thakur


People also ask

How do I change AutoFill zip code on iPhone?

Go to Safari > Preferences > AutoFill then check and see if "Using information from my contacts." If not, let's check that. You can see more information about AutoFill in. If the issue remains, let us know if this only happens with one website or multiple websites.

Why does my iPhone think I'm in a different zip code?

If you are in a web browser, generally if a zip code self-populates, it is based on your estimated location based on your ISP and/or your IP address. If one of your ISP hubs in located in the zip you provided, that is generally why this happens.

How do I change my Safari zip code?

Open the Apple menu. Open System Preferences. Click “Network.” In the dropdown menu to the right of “Location,” choose the location you want to change.

How do I create a zip code on Apple?

We recommend that you use your card billing address as your Apple ID address, otherwise you can choose your current place of residence. If there is no postal code in a region and place, use 000000 to bypass it. If your billing address don't have the post code, Please use 000000 to bypass it.


2 Answers

While the answers from alex_c and a-r-studios work well, if you don't feel like fussing with AddressBookUI or dictionaries, you can simply use the geocodeAddressString:completionHandler: method on the geocoder passing in the zip code alone which is sufficient for the lookup:

[[CLGeocoder new] geocodeAddressString:zip completionHandler:^(NSArray *placemarks, NSError *error) {
    if (placemarks.count) {
        CLPlacemark *placemark = placemarks.firstObject;

        NSString *city = placemark.locality;
        NSString *state = placemark.administrativeArea;
    }
}];

In Swift:

CLGeocoder().geocodeAddressString(zip) { (placemarks, error) in
    if let result = placemarks?.first {
        let city = result.locality
        let state = result.administrativeArea
    }
}
like image 82
Nathan Hosselton Avatar answered Oct 06 '22 01:10

Nathan Hosselton


I try to avoid Google's services because they tend to charge at a certain level of usage. Here's the solution using Apple's frameworks:

 #import <CoreLocation/CoreLocation.h>
 #import <AddressBookUI/AddressBookUI.h>

- (void)didEnterZip:(NSString*)zip
{
    CLGeocoder* geoCoder = [[CLGeocoder alloc] init];
    [geoCoder geocodeAddressDictionary:@{(NSString*)kABPersonAddressZIPKey : zip} 
      completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0) {
            CLPlacemark* placemark = [placemarks objectAtIndex:0];

            NSString* city = placemark.addressDictionary[(NSString*)kABPersonAddressCityKey];
            NSString* state = placemark.addressDictionary[(NSString*)kABPersonAddressStateKey];
            NSString* country = placemark.addressDictionary[(NSString*)kABPersonAddressCountryCodeKey];

        } else {
            // Lookup Failed
        }
    }];
}
like image 25
Danny Avatar answered Oct 06 '22 00:10

Danny