我想以编程方式检查给定号码是否在电话簿中。
我能够显示给定号码的联系人姓名(如果它存在于电话簿中)但是当电话簿中没有与该号码联系时程序崩溃... 但我想要的是显示名称,如果它存在于电话簿.....并显示数字,如果不在电话簿...... 我的代码如下....
NSMutableArray * names = [[NSMutableArray alloc] init]; NSMutableArray * phonenumbers = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSString *name;
for (id person in thePeople)
{
name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"!!!!!! name ---> %@",name);
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
int count1=ABMultiValueGetCount(multi);
NSLog(@"%d",count1);
if ([name length]>0 && count1!=0)
{
NSString *beforenumber = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);
NSLog(@" contacts:%@",beforenumber );
NSString* removed1=[beforenumber stringByReplacingOccurrencesOfString:@"-"withString:@""];
NSString* removed2=[removed1 stringByReplacingOccurrencesOfString:@")"withString:@""];
NSString* removed3=[removed2 stringByReplacingOccurrencesOfString:@" "withString:@""];
NSString* removed4=[removed3 stringByReplacingOccurrencesOfString:@"("withString:@""];
NSString* removed5=[removed4 stringByReplacingOccurrencesOfString:@"+"withString:@""];
[names addObject:name];
[phonenumbers addObject:removed5];
}
}
int barindex = -1;
barindex = [phonenumbers indexOfObject:number];
if(barindex == -1)
return number;
else
return [names objectAtIndex:barindex];
任何人都可以帮我怎么做.............
谢谢.......