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?
}
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.
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.
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.
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.
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
}
}
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
}
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With