時代遅れの ABAdressBook コードを、Objective C の現在の CNContact フレームワークに置き換えています。ホーム シティの部分を除いて、そのほとんどを整理できたので、それに焦点を当てましょう。現在、私はこのコードを持っています:
-(NSArray *)getLandAddressesForContactIOS6:(ABRecordRef)recordRef {
ABMultiValueRef addresses = ABRecordCopyValue(recordRef, kABPersonAddressProperty);
NSMutableArray *formattedAddressesResponse = [NSMutableArray array];
for(CFIndex i = 0; i < ABMultiValueGetCount(addresses); i++) {
NSString *label = (__bridge NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(addresses, i));
NSDictionary *addressComponents = (__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(addresses, i);
NSString *street = [addressComponents objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressComponents objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *formattedAddress = ABCreateStringWithAddressDictionary(addressComponents, YES);
NSMutableDictionary *currentAddressResponse = [NSMutableDictionary dictionaryWithObjectsAndKeys:
label, @"type",
label, @"label",
nil];
if (street != nil) {
[currentAddressResponse setObject:street forKey:@"street"];
}
if (city != nil) {
[currentAddressResponse setObject:city forKey:@"city"];
}
if (formattedAddress != nil) {
[currentAddressResponse setObject:formattedAddress forKey:@"formattedAddress"];
}
[formattedAddressesResponse addObject:currentAddressResponse];
}
return formattedAddressesResponse;
}
このコードは iOS 9 以降では廃止されているため、新しい連絡先フレームワークでホーム都市を取得するために必要な最も近いものは次のとおりです。
-(NSArray *)getLandAddressesForContactIOS10:(CNContact*)recordRef {
NSArray <CNLabeledValue<CNPostalAddress *> *> *addresses = recordRef.postalAddresses;
NSMutableArray *formattedAdressResponse = [NSMutableArray array];
for(CFIndex i = 0; i < addresses.count; i++) {
CNLabeledValue *addressi = [addresses objectAtIndex:i];
//NSString *city = addressi.??????; //Stuck here, don't know what else to do
CNContact から都市名を抽出するにはどうすればよいですか??