2013-06-03 5 views
0

J'ai une question. Je veux faire un bouton que lorsque je clique sur lui, il met le nouveau contact dans iPhone AdressBook. Je vois beaucoup de tutoriels mais je n'ai pas trouvé de tutoriel pour vérifier si le contact existe déjà dans l'AdressBook.Ajouter/Vérifier xcode arc

Quelqu'un peut-il m'aider à faire cela par programme? Je veux que lorsque quelqu'un clique sur le bouton, il vérifie d'abord ou le contact existe déjà dans l'AdressBook.

J'espère que quelqu'un peut m'aider.

Merci beaucoup ;-)

Répondre

0
ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(Nil, Nil); 
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressbook); 
NSMutableArray *namearray=[[NSMutableArray alloc] init]; 
for(id person in people){ 
     NSString *firstNameString = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(person),kABPersonFirstNameProperty); 
    NSLog(@"%@",firstNameString); 
     [namearray addObject:firstNameString]; 
    } 
    NSLog(@"%@",namearray); 
NSString *yourString = name you want to add; 
BOOL identicalStringFound = NO; 
for (NSString *someString in namearray) { 
    if ([someString isEqual:yourString]) { 
     identicalStringFound = YES; 
     break; 
    } 
} 
if(identicalStringFound) 
{ 
    //code you want to perform 
} 
else 
{ 
    //code you want to perform 
} 
0

Check contact est sortie ou non par contact Nombre

Remarque: Ne remplacez variables checkingPhoneNumber par votre contact vérification numéro

ABAddressBookRef * addressbook = ABAddressBookCreateWithOptions(Nil, Nil); 
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressbook); 
NSMutableArray *phoneArray=[[NSMutableArray alloc] init]; 

for(id person in people) 
{ 
    // get person contact number 
    ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty); 
    NSString* [email protected]""; 
    NSString* mobileLabel; 

    for (int i=0; i < ABMultiValueGetCount(phones); i++) 
    { 
     mobileLabel = (__bridge 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 = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i); 
     NSLog(@"%@", mobile); 

     // remove all spaces bracket from contact number 
     NSMutableString *newPhoneStr = [[NSMutableString alloc] init];; 
     int j = [mobile length]; 
     for (int i=0; i<j; i++) 
     { 
      if ([mobile characterAtIndex:i] >=48 && [mobile characterAtIndex:i] <=59) 
      { 
       [newPhoneStr appendFormat:@"%c",[mobile characterAtIndex:i]]; 
      } 
     } 
     //add contact into phoneArray 
     [phoneArray addObject:newPhoneStr]; 
    } 
} 
NSLog(@"%@",phoneArray); 

BOOL identicalStringFound = NO; 

// remove all spaces bracket from contact number which is check 
NSMutableString *newCheckingPhoneNumberStr = [[NSMutableString alloc] init]; 
int j = [checkingPhoneNumber length]; 
for (int i=0; i<j; i++) 
{ 
    if ([checkingPhoneNumber characterAtIndex:i] >=48 && [[profileDetailsDict valueForKey:@"mobile"] characterAtIndex:i] <=59) 
    { 
     [newCheckingPhoneNumberStr appendFormat:@"%c",[checkingPhoneNumber characterAtIndex:i]]; 
    } 
} 

for (NSString *contact in phoneArray) 
{ 
    if ([contact isEqual:newCheckingPhoneNumberStr]) 
    { 
     identicalStringFound = YES; 
     break; 
    } 
} 
if(identicalStringFound) 
{ 
    // checkingPhoneNumber is exit 
} 
else 
{ 
    // checkingPhoneNumber is not exit 
} 
Questions connexes