开发者

checking whether the number is in phone contacts are not

i want to check whether the given number is in phone book or not programatically .

i am able to display the contact name of the given number if it is exists in phone book but the program crashes when there is no contact with that number in phone book ... but what i want is to display name if it exists in phone book..... and display the number if if is not in phone book...... and my code is as follows....

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* remo开发者_如何学Goved3=[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];

can any one please help me how to do that.............

thank you.......


The code would be improved but I think that is what you need. Sample Code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜