gpt4 book ai didi

ios - 从 iOS 中的邮政编码自动填充城市和州

转载 作者:技术小花猫 更新时间:2023-10-29 10:40:48 24 4
gpt4 key购买 nike

我的 View 中有三个文本字段。1. 邮政编码,2. 城市和 3. 州。

如何在 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?
}

最佳答案

我尽量避免使用 Google 的服务,因为它们往往会按一定的使用量收费。这是使用 Apple 框架的解决方案:

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

关于ios - 从 iOS 中的邮政编码自动填充城市和州,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23387162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com