HI here is the below function where i am collecting NSString values like firstname and last name from the function and assign in it array
- (void)getPersonOutOfAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil)
{
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
NSLog(@" Result :%d",[allContacts count]);
for (i = 0; i < [allContacts count]; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSMutableArray *name = [NSMutableArray array];
[name addObject:firstName];
[name addObject:lastName];
[self displaynames:name];
//NSLog(@"FirstNAme::::: LastNAME::::MobileNUMb::::EMail::: %@ %@",firstName,lastName);
ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (int k=0;k<ABMultiValueGetCount(mobile); k++)
{
NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
// NSLog(@"mobile number: %@",mobileNo);
}
}
-(void)displaynames:(NSMutableArray*)names{
for(NSMutableArray* name in names){
NSLog(@"MyResult:%@ %@",[names objectAtIndex:0],[names objectAtIndex:1]);
messageToSend = [messageToSend stringByAppendingString:[names objectAtIndex:0]];
messageToSend = [messageToSend stringByAppendingString:[names objectAtIndex:1]];
}
}
And also to send data
- (IBAction)sendContacts:(id)sender
{
NSData *textData = [self.messageToSend dataUsingEncoding:NSASCIIStringEncoding];
//send data to all connected devices
[self.session sendDataToAllPeers:textData withDataMode:GKSendDataReliable error:nil];
}
here messageToSend is a NSMutableString
So in the above code i am collecting all firstname and lastname and placing in it array.When i run this in iphone it shows error [__NSArrayM insertObject:atIndex:]: object cannot be nil'
After NSLog(@"Myresult"); it is displaying firstname and lastname twice.so can anyone tel me where i am goin wrong and same way how do i collect phonenumber as wel.what changes i have to do in above code to avoid that error and it displaying only few names not all.
It displays
2013-06-03 11:40:12.524 GetContacts[1173:1bb03] Succesful.
2013-06-03 11:40:12.539 getlist[1173:1bb03] Result :2
2013-06-03 11:40:12.567 getlist[1173:1bb03] MyResult:john cena
2013-06-03 11:40:12.568 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.569 getlist[1173:1bb03] MyResult:john cena
2013-06-03 11:40:12.571 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.734 getlist[1173:1bb03] MyResult:peter john
2013-06-03 11:40:12.735 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.736 getlist[1173:1bb03] MyResult:peter john
2013-06-03 11:40:12.737 getlist[1173:1bb03] Successsssss::::::