program crashes when there is no phone number in phone contacts in iphone
i am able to read contacts from phone book and display them in tableview ......
the program is working successful when there is both phone number and namr in contact list and crashes if there is no name or phonenumber in contact list....
can any one please help me how to stop that and how to check these condition to display those names in table view which contain both phone number and name.... and my code is as follows
wantedname=[[NSMutableArray alloc] init];
wantednumber=[[NSMutableArray alloc] init];
ABAddressBo开发者_StackOverflowokRef 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);
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:@""];
}
importcontacts *phonecontacts = [[importcontacts alloc] init];
phonecontacts.names = wantedname;
phonecontacts.phonenumbers = wantednumber;
You have to check whether person has contact number in list or not.. Try to use below modified code
wantedname=[[NSMutableArray alloc] init];
wantednumber=[[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 (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:@""];
}
}
importcontacts *phonecontacts = [[importcontacts alloc] init]; phonecontacts.names = wantedname; phonecontacts.phonenumbers = wantednumber;
精彩评论