Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi i am doing an apps where i want to collect names and phonenumber from the list and add it into array.So how do i do this.I could retrive Firstname and lastname but i am not gettig how to add Phonenumber in the below code as it is in the different For Loop it might look simple but i am stuck as i am new.

    for (i = 0; i < [list count]; i++)
    {
         NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);

        NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

      NSMutableArray *name = [NSMutableArray array];
       if(firstName != nil)
            [name addObject:firstName];

        if(lastName != nil)
            [name addObject:lastName];*/

        [self displaynames:name];


        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]);

}

So in the above code i am abe to get firstname and lastname from the list and add into array,similarly how do i get mobileno and add into same array and get the result in displaynames function as it is other for loop can anyone please edit the code and tel what changes i have to make in the above code.Also in Result everything is being Displayed Twice why so?

share|improve this question

4 Answers

So, instead using Array you have to use Dictionary to store FirstName, LastName and MobileNo with keyvalue Pair. If you have multiple user than use Array as upper layer, means add your user dictionary into array and when ever you want a user write the code:

for(NSDictionary *userDic in yourArray)
{
    NSString *fName = [userDic valueForKey:@"FirstName"];
    ...
}

This is one of the way...

share|improve this answer

Try the following steps:

NSMutableArray *firstNames;
NSMutableArray *LastNames;
NSMutableArray *Mobile_numbers;
NSMutableArray *type_array;
NSMutableArray *firstandlast;

........

-(void)get_arr
{
 ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);    
    NSUInteger index = 0;
    firstNames = [[NSMutableArray alloc] init];
   Mobile_numbers = [[NSMutableArray alloc] init];
    type_array=[[NSMutableArray alloc]init ];
    LastNames=[[NSMutableArray alloc]init ];
   NSMutableArray *firstandlast;

   @try
    {
        for(index = 0; index<=([arrayOfPeople count]-1); index++)
        {

            ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];

            NSString *type;

            ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(currentPerson, kABPersonPhoneProperty);

            for (int i=0; i < ABMultiValueGetCount(phones); i++)
            {

                //NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
                ////NSLog(@"%@", phone);
                mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);


                //NSLog(@"MOG:%@",mobileLabel);


                if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]||[mobileLabel isEqualToString:@"_$!<Main>!$_"])
                {
                    //NSLog(@"mobile:");
                    type=@"mobile";
                }
                else if ([mobileLabel  isEqualToString:@"_$!<Work>!$_"])
                {
                    //NSLog(@"Work:");
                    type=@"Work";
                }
                else if ([mobileLabel isEqualToString:@"_$!<Home>!$_"])
                {
                    //NSLog(@"Home:");
                    type=@"Home";
                }

                else if ([mobileLabel isEqualToString:@"_$!<Other>!$_"] )
                {
                    //NSLog(@"Other:");
                    type=@"Other";
                }


                mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);

                //NSLog(@"GG:%@",mobile);


                mobile = [mobile stringByReplacingOccurrencesOfString:@"-"
                                                                             withString:@""];

                mobile = [mobile stringByReplacingOccurrencesOfString:@"("
                                                                             withString:@""];

                mobile = [mobile stringByReplacingOccurrencesOfString:@")"
                                                                             withString:@""];

                mobile = [mobile stringByReplacingOccurrencesOfString:@" "
                                                                             withString:@""];

                [Mobile_numbers addObject:mobile];
                [type_array addObject:type];

                NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);

                ////NSLog(@"NAME:%@",currentFirstName);

                if ([currentFirstName length]!=0)
                {
                    //NSLog(@"NAME:%@",currentFirstName);
                    [firstNames addObject:currentFirstName];
                }
                else
                {
                    //NSLog(@"NAME:DUMMY");
                    currentFirstName=@"";
                    [firstNames addObject:currentFirstName];
                }

                NSString *currentLast = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonLastNameProperty);

                ////NSLog(@"NAME:%@",currentFirstName);

                if ([currentLast length]!=0)
                {
                    //NSLog(@"NAME:%@",currentLast);
                    [LastNames addObject:currentLast];
                }
                else
                {
                    //NSLog(@"NAME:DUMMY");
                    currentLast=@"";
                    [LastNames addObject:currentLast];
                }


                   NSString *temp_f_l=[NSString stringWithFormat:@"%@ %@",currentFirstName,currentLast];
                [firstandlast addObject:temp_f_l];
            }


            NSLog(@"MOB:%@",Mobile_numbers);
            NSLog(@"TYPE:%@",type_array);



            NSLog(@"FN:%@",firstNames);
            NSLog(@"FN:%@",LastNames);


             NSLog(@"FN&LN:%@",firstandlast);

        }

    }
    @catch (NSException *exception)
    {
        //NSLog(@"CATCH");


    }

}

In my Application. I am using this code to store Addressbook contents to Array. I hope it help for you.

share|improve this answer
i want to collect all firstname,lastname and mobile number from the list and send it using peer session so how do i collect all firstname and lastname mobile number – user2454248 1 hour ago
My code note First name,last name and mobile numbers array. Try this code in your application and check with NSLog it must help. – user1779765 1 hour ago
Its working?. let me i will help – user1779765 1 hour ago
No its not..Lot of errors i am facing again i need to change complete code. – user2454248 1 hour ago
Oh, its not ARC. Its MRC based code. – user1779765 1 hour ago

You can try to add the user info in a dictionary or create a model user object and store in the array. So all the information stays encapsulated in a single object.

self.users = [NSMutableArray array];
for (i = 0; i < [list count]; i++)
{
        NSString *firstName = ...

        NSString *lastName = ...

        NSString *mobileNumber = ...

        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        if(firstName)
            userInfo[@"FirstName"] = firstName;
        if(lastName)
            userInfo[@"LastName"] = lastName;
        if(mobileNumber)
            userInfo[@"MobileNumber"] = mobileNumber;

        [self.users addObject:userInfo];

    }

You can enumerate using

[self.users enumerateObjectsUsingBlock:^(NSDictionary * userInfo, NSUInteger idx, BOOL *stop) {

        NSString *firstName = userInfo[@"FirstName"];
        NSString *mobileNumber = userInfo[@"MobileNumber"];

    }];

Searching for a single user for a name

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FirstName == %@",@"aUserName"];
NSDictionary *userInfo = [[self.users filteredArrayUsingPredicate:predicate]lastObject];
share|improve this answer
So above method is not right??i want this way because i want to send the result Data to some other functionality..So can you please edit the code and i am new to ios.. – user2454248 2 hours ago
so if i want to use userinfo outside this function for further functionality how do i use this??bcoz i need to send all this data in peer to peer session thats why.Mobile number is in different for loop in the above code.Also what is users in the above code. – user2454248 2 hours ago
You can see that all info is added in a property array. If you want to display in a serial way you can do enumeration. If you want to access a specific info, you can filter using NSPredicate. – Anupdas 2 hours ago
What is users in the above code. – user2454248 2 hours ago
I have edited the code above can you please check and tell me right and what else i should change.What is self.users ? – user2454248 2 hours ago
show 1 more comment
NSMutableDictionary *contactInfo=[[NSMutableDictionary alloc] init];

for (int i = 0; i < [list count]; i++)
{
    NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

    if (![firstName isEqualToString:@""]) {
        [contactInfo setValue:firstName forKey:@"firstName"];
    }

    if (![lastName isEqualToString:@""]) {
        [contactInfo setValue:lastName forKey:@"lastName"];
    }

    NSMutableArray *mobileNoArray=[[NSMutableArray alloc] initWithCapacity:ABMultiValueGetCount(mobile)];

    ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);

    for (int k=0;k<ABMultiValueGetCount(mobile); k++)
    {
        NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
        [mobileNoArray addObject:mobileNo];
    }

    if (mobileNoArray.count!=0) {
        [contactInfo setObject:mobileNoArray forKey:@"mobileNo"]
    }
}

NSLog(@"contact info ==  %@",contactInfo);
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.