Anatoliy Gatt
Anatoliy Gatt

Reputation: 2491

Passing NSMutableArray to the server

I'm currently working with AddressBook in iOS, I'm getting the emails from all the contacts of the user, and putting them into the NSMutableArray. Now I must to pass this array to the server[PHP]. But the problem that is the emails are in the double quotes, so it looks something like this:

(
  "user@gmail.com",
  "user2@gmail.com",
  "user3@gmail.com"
)

That is my code:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
    accessGranted = YES;
}

if (accessGranted) {
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    _usersEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)];
    for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
        ABRecordRef person = CFArrayGetValueAtIndex(people, i);
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
            NSData* email = [(__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j) dataUsingEncoding:NSUTF8StringEncoding];
            [_usersEmails addObject:email];
        }
        CFRelease(emails);
    }
    CFRelease(addressBook);
    CFRelease(people);

Web-programmer that was implementing the server-side, told me that I must to pass the array of strings without the double quotes.

How can I remove the double quotes in each string of my array?

Update:

This is the piece of the actual data that had been received by the server:

\"user@gmail.com\"','\"user@gmail.com\"','\"user@gmail.com\"','\"user@me.com\"

Upvotes: 0

Views: 234

Answers (3)

Levi
Levi

Reputation: 7343

You can use the NSString's substringFromIndex and substringToIndex methods to eliminate the first and last characters. Something like [[string substringToIndex:string.length - 1] substringFromIndex:1];

EDIT:

And can't you check/remove the quotation marks here:

NSData* email = [(__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j) dataUsingEncoding:NSUTF8StringEncoding];

by writing something like:

NSString *string = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
NSString *stringWithoutQuotations = [[string substringToIndex:string.length - 1] substringFromIndex:1];
NSData* email = [stringWithoutQuotations  dataUsingEncoding:NSUTF8StringEncoding];

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47759

Do this:

for (NSString* aString in _userEmails) {
    NSLog(@"The string is %@", aString);
}

You will see that there are no quotes.

Upvotes: 1

nielsbot
nielsbot

Reputation: 16031

The quotes are shown when NSString is printed to the console--I don't think your strings actually contain quotation marks..

Try using the debugger inspector instead to double-check.

Upvotes: 1

Related Questions