Pergunta

Estou tentando buscar a propriedade de e-mail selecionada no retorno de chamada do delegado mencionado abaixo

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
        if (property==kABPersonEmailProperty) {
            ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
            if (ABMultiValueGetCount(emails) > 0) {
                NSString *email = (__bridge_transfer NSString*)
                ABMultiValueCopyValueAtIndex(emails, ABMultiValueGetIndexForIdentifier(emails,identifier));
                [recipientEmail setText:email];
                [peoplePicker dismissViewControllerAnimated:YES completion:nil];
            }
             CFRelease(emails);
        }
        return NO;
    }

Mas se eu selecionar a propriedade email do contato vinculado (tendo um único email), obtenho o identificador como 0 e, como resultado, obtenho o primeiro email-id do contato principal.Por exemplo:John - john@gmail.com john26@gmail.com Roger (contato vinculado) - roger@gmail.com

Quando seleciono roger@gmail.com recebo john@gmail.com.

Foi útil?

Solução

Eu tenho esse mesmo problema.Recebo o e-mail errado quando seleciono contas com vários endereços de e-mail.Quando percorro o ABMutableMultiValueRef selecionado...

for (CFIndex ix = 0; ix < ABMultiValueGetCount(emails); ix++) {
   CFStringRef label = ABMultiValueCopyLabelAtIndex(emails, ix);
   CFStringRef value = ABMultiValueCopyValueAtIndex(emails, ix);
   NSLog(@"I have a %@ address: %@", label, value);
   if (label) CFRelease(label);
   if (value) CFRelease(value);
}

...Recebo várias ocorrências do mesmo endereço, mas algumas com rótulos nulos.

> I have a (null) address: john@home.com
> I have a _$!<Work>!$_ address: jsmith@work.com
> I have a _$!<Home>!$_ address: john@home.com

A solução alternativa que tentarei é filtrar primeiro os rótulos nulos, ver se o ABMultiValueIdentifier 'se ajusta' e, se não, reverter para os rótulos nulos.A menos que você tenha encontrado algo?

Editar:isso funciona para mim.

    NSMutableArray *labeled = [NSMutableArray new];
    ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex ix = 0; ix < ABMultiValueGetCount(emails); ix++) {
        CFStringRef label = ABMultiValueCopyLabelAtIndex(emails, ix);
        CFStringRef value = ABMultiValueCopyValueAtIndex(emails, ix);
        if (label != NULL) {
            [labeled addObject:(NSString *)CFBridgingRelease(value)];
        }
        if (label) CFRelease(label);
        if (value) CFRelease(value);
    }
    NSString *email;
    if (labeled.count > identifier) {
        email = [labeled objectAtIndex:identifier];
    } else {
        CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, identifier);
        email = (NSString *)CFBridgingRelease(emailRef);
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top