2

時代遅れの 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 から都市名を抽出するにはどうすればよいですか??

4

2 に答える 2

1
NSArray *addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"];

        if (!(addresses == nil) && addresses.count > 0)
        {
            for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
            {      
                NSString *city = labeledValue.value.city;
                NSLog(@"City = %@",city);

                NSString *street = labeledValue.value.street;
                NSLog(@"Street = %@",street);

                NSString *state = labeledValue.value.state;
                NSLog(@"State = %@",state);

                NSString *postalCode = labeledValue.value.postalCode;
                NSLog(@"PostalCode = %@",postalCode);

                NSString *ISOCountryCode = labeledValue.value.ISOCountryCode;
                NSLog(@"ISOCountryCode = %@",ISOCountryCode);   
            }
        }
        else
        {
            NSLog(@"No addresses for name = %@",strname);
        }            
于 2018-11-15T09:43:27.497 に答える
1
-(void )getLandAddressesForContactIOS10:(CNContact*)contact
{
    for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
    {

            NSLog(@"%@",labeledValue.value.city);

            NSLog(@"%@",labeledValue.value.street);

            NSLog(@"%@",labeledValue.value.state);

            NSLog(@"%@",labeledValue.value.postalCode);

            NSLog(@"%@",labeledValue.value.ISOCountryCode);

    }
}
于 2016-12-15T09:50:26.100 に答える