2011-03-20 7 views
15

Tengo un método para obtener todos los números de teléfono de la libreta de direcciones en el iPhone, pero ¿hay alguna manera de obtener la etiqueta del número de teléfono? Por ejemplo, puede hacer esto: enter image description hereObtener la etiqueta de número de teléfono iPhone de la Libreta de direcciones

Y yo estaría buscando modificar mi método para imprimir la etiqueta (como iPhone/Inicio/móvil/etc).

ABAddressBookRef addressBook = ABAddressBookCreate(); 
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook); 
CFIndex n = ABAddressBookGetPersonCount(addressBook); 

for(int i = 0 ; i < n ; i++) 
{ 
    ABRecordRef ref = CFArrayGetValueAtIndex(all, i); 
    NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
    NSLog(@"Name %@", firstName); 

    ABMultiValueRef *phones = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
    { 
     NSString *phoneLabel = @""; // ??? 

     CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); 
     //CFRelease(phones); 
     NSString *phoneNumber = (NSString *)phoneNumberRef; 
     CFRelease(phoneNumberRef); 
     NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 
     [phoneNumber release]; 
    } 
} 

Respuesta

60

Utilice simplemente -

ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty); 
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) 
{ 
    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); 
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j); 
    NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 
    //CFRelease(phones); 
    NSString *phoneNumber = (NSString *)phoneNumberRef; 
    CFRelease(phoneNumberRef); 
    CFRelease(locLabel); 
    NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 
    [phoneNumber release]; 
} 

EDITAR Por favor ver las notas para esta respuesta acerca CFBridgingRelease y __bridge_transfer.

+0

¡Gracias, eso fue todo! –

+0

Bienvenido, me alegro de que haya ayudado – shannoga

+3

En lugar de usar (NSString *) para convertir al uso de cadenas (__bridge_transfer NSString *). – Dev2rights

-4

lo siguiente debería ayudar:

NSArray* AccountEmailAddresses(void) 
{ 
    NSMutableArray *emailAddresses = [NSMutableArray array]; 
    @try 
    { 
     Class MailComposeController = NSClassFromString(@"MailComposeController") ?: NSClassFromString(@"MFMailComposeController"); 
     NSArray *accountEmailAddresses = [MailComposeController performSelector:@selector(accountEmailAddresses)]; 
     for (id address in accountEmailAddresses) 
     { 
      if ([address isKindOfClass:[NSString class]]) 
       [emailAddresses addObject:address]; 
     } 
    } 
    @catch (NSException *e) {} 

    return [NSArray arrayWithArray:emailAddresses]; 
} 


ABRecordRef ABGetMe(ABAddressBookRef addressBook) 
{ 
    ABRecordRef me = NULL; 
    NSArray *accountEmailAddresses = AccountEmailAddresses(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFIndex peopleCount = CFArrayGetCount(people); 
    for (CFIndex i = 0; i < peopleCount; i++) 
    { 
     ABRecordRef record = CFArrayGetValueAtIndex(people, i); 
     ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty); 
     if (emails) 
     { 
      CFIndex emailCount = ABMultiValueGetCount(emails); 
      for (CFIndex j = 0; j < emailCount; j++) 
      { 
       CFStringRef email = ABMultiValueCopyValueAtIndex(emails, j); 
       if (email) 
       { 
        if ([accountEmailAddresses containsObject:(id)email]) 
         me = record; 

        CFRelease(email); 
       } 
       if (me) 
        break; 
      } 
      CFRelease(emails); 
     } 
     if (me) 
      break; 
    } 

    return me; 
} 
+2

¿qué? el usuario preguntó sobre los números de teléfono, no las direcciones de correo electrónico – lensovet

3
//get the particular contact or email from phone book 

    - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
    { 
     // Name of contact. 

     NSString* name = (NSString *)ABRecordCopyCompositeName(person); 

     // Numbers of selected contact 

     ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); 

     NSMutableString *mobile = [[NSMutableString alloc] init]; 
     NSMutableString *office = [[NSMutableString alloc] init]; 

     // Getting if Mobile, Office(work) numbers exist 

     for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++) 
     { 
      // Number in contact details of current index 

     CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, numberIndex); 

     // Label of Phone Number 

     CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, numberIndex); 
     NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

     // Phone number 

     NSString *phoneNumber = (NSString *)phoneNumberRef; 

     // Release Phone Number and locationLabel reference object 

     CFRelease(phoneNumberRef); 
     CFRelease(locLabel); 

     NSLog(@" - %@ (%@)", phoneNumber, phoneLabel); 

     if ([phoneLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Mobile number saving. 
     { 
      [mobile appendFormat:@"%@", phoneNumber]; 
     } 
     else if ([phoneLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office number saving. 
     { 
      [office appendFormat:@"%@", phoneNumber]; 
     } 

     [phoneNumber release]; 
    } 
    CFRelease(phones); 

    // Emails of selected contact 

    ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 

    NSMutableString *generalMail = [[NSMutableString alloc] init]; 
    NSMutableString *officeMail = [[NSMutableString alloc] init]; 

    // Getting if Home, Office(work) mails exist 

    for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(emails); numberIndex++) 
    { 
     // Mail in contact details of current index 

     CFStringRef mailRef = ABMultiValueCopyValueAtIndex(emails, numberIndex); 

     // Label of Phone Number 

     CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(emails, numberIndex); 
     NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

     // Phone number 

     NSString *mail = (NSString *)mailRef; 

     // Release Phone Number and locationLabel reference object 

     CFRelease(mailRef); 
     CFRelease(locLabel); 
     NSLog(@" - %@ (%@)", mail, mailLabel); 

     if ([mailLabel isEqualToString:NSLocalizedString(@"mobile", nil)])// Home mail. 
     { 
      [generalMail appendFormat:@"%@", mail]; 
     } 
     else if ([mailLabel isEqualToString:NSLocalizedString(@"work", nil)])// Office(Work) mail. 
     { 
      [officeMail appendFormat:@"%@", mail]; 
     } 

     [mail release]; 
    } 
    CFRelease(emails); 

    [mobile release]; 
    [office release]; 

    [generalMail release]; 
    [officeMail release]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
    return NO; 
} 
+0

si este código se ejecuta con deutsch, luego "mobile" cambia a "mobil"! . ¿Cómo puedo manejar esos casos? Es posible que no pueda verificar todas las cadenas – BaSha

+0

Ok. Cree un archivo de localización en su proyecto para el idioma que desee. En ese archivo define mobile = 'mpbiel'. Estoy modificando de acuerdo con el código anterior. – abhi

+0

En realidad, he resuelto el problema, tenemos que comparar la etiqueta local en lugar de la etiqueta localizada -> let locLabel: CFStringRef = ABMultiValueCopyLabelAtIndex (phones, numberIndex) .takeUnretainedValue() como CFStringRef; if (String (locLabel) == String (kABHomeLabel)) {} – BaSha

0

Si va a añadir registros en la agenda telefónica, estas constantes predefinidas puede ser lo que usted quiere, kABPersonPhoneMobileLabel, kABPersonPhoneIPhoneLabel, que se definen en el archivo ABPerson.h .

Cuestiones relacionadas