-1

Everybody.. I am trying to develop an app for iPhone which basicly deals with ABAddressBook and contacts data... My goal is to send all the contacts data (at first step, names and phones) in a file by e-mail..

Now, I am trying to reach the contacts data, and I want to add them to two different arrays, names and phones arrays..

At first, I am trying to see all the datas in the screen when i pressed a button "List Contacts". the datas should be seen on the screen. and then when i pressed the second button "Send Contacts", it should send the file to an e-mail account.. This is how my apps will work..

I am having problems at showing the data on the screen.. I wrote something but it doesn't give anything on the screen in a textView.. Can you help me to solve this problem?

Here's the code for listing the contacts (listCon method):


-(IBAction)listCon:(id)sender
{

    NSMutableArray *names = [[NSMutableArray alloc] init];

    NSMutableArray *numbers1= [[NSMutableArray array] init];

    NSMutableArray *numbers2= [[NSMutableArray array] init];

    NSMutableArray *numbers3= [[NSMutableArray array] init];

    ABAddressBookRef addressBook = ABAddressBookCreate();

    if (addressBook != nil)
    {

        NSLog(@"Successfully accessed the address book.");

        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople= ABAddressBookGetPersonCount(addressBook);

        NSUInteger peopleCounter = 0;
        for (peopleCounter = 0;peopleCounter < nPeople; peopleCounter++)
        {

            ABRecordRef thisPerson = CFArrayGetValueAtIndex(allPeople,peopleCounter);

            NSString *contactFirstLast = [NSString stringWithFormat:@"%,%",ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];

            [names insertObject:contactFirstLast atIndex:peopleCounter];

            ABMultiValueRef phoneNumbers = ABRecordCopyValue(thisPerson,kABPersonPhoneProperty);

            NSString *firstPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

            NSString *secondPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 1);

            NSString *thirdPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 2);

            [numbers1 insertObject:firstPhone atIndex:peopleCounter];
            [numbers2 insertObject:secondPhone atIndex:peopleCounter];                             
            [numbers3 insertObject:thirdPhone atIndex:peopleCounter];
    }          
}

myView.text=[names componentsJoinedByString:@"\n\n"];     

myView.text=[numbers1 componentsJoinedByString:@"\n\n"];    

myView.text=[numbers2 componentsJoinedByString:@"\n\n"];

myView.text=[numbers3 componentsJoinedByString:@"\n\n"];
}   
pasawaya
  • 11,364
  • 7
  • 50
  • 92
mtlcck
  • 9
  • 2

1 Answers1

0

Just glancing at your code, you can't do this:

NSString *contactFirstLast = [NSString stringWithFormat:@"%,%",ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];

There are several errors: first off % in your stringWithFormat: is not a format specifier; you probably are thinking of %@. Second off, copying the value of kABPersonFirstNameProperty will return a CFStringRef, and that's not what you want to display the name in a text field. You'll have to toll-free bridge the result of ABRecordCopyValue(). You can do this by adding this line - (__bridge_transfer NSString *) - in front of your ABRecordCopyValue()'s. With all the corrections, it should look like this:

NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@", (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];

Hope this help (might not cover all errors)!

pasawaya
  • 11,364
  • 7
  • 50
  • 92