2010-08-23 15 views
5

Utilizo el siguiente código para configurar la recuperación del número de teléfono en mi aplicación.Cómo recuperar números de móvil desde Contactos de iPhone.

CFStringRef addressBookMobile; 
ABRecordRef person; 
NSString *mobile; 

person = CFArrayGetValueAtIndex(people, i); 
addressBookMobile = ABRecordCopyValue(person, kABPersonPhoneProperty); 
mobile = [NSString stringWithFormat:@"%@", addressBookMobile]; 

La etiqueta de los contactos es 'móvil'. Sin embargo, cuando uso el NSLog(@"%@", mobile);. Muestra el <NSCFType: 0x802ffc0>. ¿Hay algún problema para mi código?

¿Debo usar el const CFStringRef kABPersonPhoneMobileLabel y cómo utilizar? Como si lo reemplazo como el código anterior, tiene el error. ¿Alguien puede ayudarme? Gracias.

Respuesta

2
ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty); 
CRStringRef mobileNumber; 
CRStringRef mobileLabel; 
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { 
    mobileLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i); 
    if ([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) { 
     mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i); 
     break; 
    } 
} 
5

Los números de teléfono de una persona en la libreta de direcciones se encuentran en la forma de una propiedad de múltiples valores.

En su caso, usted debe tener algo como lo siguiente (no lo he probado, escribiendo directamente aquí, así que no sé si se compila y/o trabajos):

ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty); 
NSString *mobileNumber; 
NSString *mobileLabel; 
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { 
    mobileLabel = (NSString *)ABMultiValueCopyLabelAtIndex(mobilePhones, i); 
    if ([mobileLabel isEqualToString:@"mobile"]) { 
     mobileNumber = (NSString*)ABMultiValueCopyValueAtIndex(mobilePhones,i); 
     break; 
    } 
} 
+0

Lancu, gracias por su respuesta. Pero creo que la solución puede ser incorrecta. el resultado es el mismo y quiero preguntar, ¿qué es MobilePhones en el código? Gracias. – Questions

+0

Lo siento Mark, debería haber sido phoneNumbers :-). Como dije, lo escribí directamente en el formulario, no en Xcode :-). Entonces en lugar de teléfonos móviles, reemplace con números de teléfono y debería ser todo bien. –

16

comprobar el ABPerson Refrence y Usted need't usar @ "$ $ !!", pero kABPersonPhoneMobileLabel. el ejemplo es:

ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty); 
    NSString* [email protected]""; 
    NSString* mobileLabel; 
    for (int i=0; i < ABMultiValueGetCount(phones); i++) { 
     //NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i); 
     //NSLog(@"%@", phone); 
     mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i); 
     if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) { 
      NSLog(@"mobile:"); 
     } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) { 
      NSLog(@"iphone:"); 
     } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) { 
      NSLog(@"pager:"); 
     } 
     [mobile release]; 
     mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i); 
     NSLog(@"%@", mobile); 
    } 
+0

razón por la que me muestra error en esta línea de teléfonos ABMultiValueRef = (ABMultiValueRef) ABRecordCopyValue (persona, kABPersonPhoneProperty); NSString * mobile = @ ""; dice persona no declarada. estoy usando ios 5 – user2189388

0

Ésta es sólida para ARC 64 bits iOS8:

- (NSArray *)phoneNumbersOfContactAsStrings:(ABRecordRef)contactRef { 

NSMutableArray *mobilePhones = [NSMutableArray arrayWithCapacity:0]; 

ABMultiValueRef phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty); 
NSArray *allPhoneNumbers = (NSArray *)CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones)); 

for (NSUInteger i=0; i < [allPhoneNumbers count]; i++) { 
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) { 
     [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))]; 
    } 
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) { 
     [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))]; 
    } 
} 

CFRelease(phones); 
return mobilePhones; 
} 
Cuestiones relacionadas