0

我目前正在 iOS 中使用 AddressBook,我正在从用户的所有联系人那里获取电子邮件,并将它们放入NSMutableArray. 现在我必须将此数组传递给服务器[PHP]。但问题是电子邮件在双引号中,所以它看起来像这样:

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

那是我的代码:

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 程序员告诉我,我必须传递不带双引号的字符串数组。

如何删除数组每个字符串中的双引号?

更新:

这是服务器接收到的实际数据:

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

3 回答 3

1

您可以使用 NSString 的substringFromIndexsubstringToIndex方法来消除第一个和最后一个字符。就像是[[string substringToIndex:string.length - 1] substringFromIndex:1];

编辑:

你不能在这里检查/删除引号:

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

通过写类似的东西:

NSString *string = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
NSString *stringWithoutQuotations = [[string substringToIndex:string.length - 1] substringFromIndex:1];
NSData* email = [stringWithoutQuotations  dataUsingEncoding:NSUTF8StringEncoding];
于 2012-12-18T19:16:08.463 回答
1

将 NSString 打印到控制台时会显示引号-我认为您的字符串实际上不包含引号..

尝试使用调试器检查器进行仔细检查。

于 2012-12-18T19:21:13.450 回答
1

做这个:

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

你会看到没有引号。

于 2012-12-18T19:25:23.383 回答