从iphone中检索联系人

时间:2014-01-18 10:53:01

标签: iphone ios7 xcode5

我需要在UITableView中显示用户手机中可用的所有联系人列表。我可以成功检索contactName,PhoneNumber。但是当我将数据添加到NSMutableArray时,它会添加一些不寻常的字符。不能理解问题所在是

-(void)viewDidLoad
{
  Names=[[NSMutableArray alloc]init];
    ABAddressBookRef allPeople = ABAddressBookCreate();
    CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
    CFIndex numberOfContacts  = ABAddressBookGetPersonCount(allPeople);

    NSLog(@"numberOfContacts---%ld",numberOfContacts);


    for(int i = 0; i < numberOfContacts; i++){
        NSString* name = @"";
        NSString* phone = @"";
        NSString* email = @"";

        ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
        ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
        ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);

        ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);\
        ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);

        NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
        NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);


        if (fnameProperty != nil) {
            name = [NSString stringWithFormat:@"%@", fnameProperty];
        }
        if (lnameProperty != nil) {
            name = [name stringByAppendingString:[NSString stringWithFormat:@" %@", lnameProperty]];
        }

        if ([phoneArray count] > 0) {
            if ([phoneArray count] > 1) {
                for (int i = 0; i < [phoneArray count]; i++) {
                    phone = [phone stringByAppendingString:[NSString stringWithFormat:@"%@\n", [phoneArray objectAtIndex:i]]];
                }
            }else {
                phone = [NSString stringWithFormat:@"%@", [phoneArray objectAtIndex:0]];
            }
        }

        if ([emailArray count] > 0) {
            if ([emailArray count] > 1) {
                for (int i = 0; i < [emailArray count]; i++) {
                    email = [email stringByAppendingString:[NSString stringWithFormat:@"%@\n", [emailArray objectAtIndex:i]]];
                }
            }else {
                email = [NSString stringWithFormat:@"%@", [emailArray objectAtIndex:0]];
            }
        }

        NSLog(@"NAME : %@",name);
        NSLog(@"PHONE: %@",phone);
        NSLog(@"EMAIL: %@",email);
        NSLog(@"\n");

        NSMutableDictionary *d=[[NSMutableDictionary alloc]init];
        [d setObject:name forKey:@"Name"];
        [d setObject:phone forKey:@"Phone"];
        [d setObject:email forKey:@"Email"];


        [Names addObject:d];



    }
    NSLog(@"%@",Names);

这是名称的NSLog:

{
    Email = "";
    Name = Hk;
    Phone = "(975)\U00a0050-1890";    //Actual Number (975)050-1890
},
    {
    Email = "";
    Name = Nik;
    Phone = "1\U00a0(234)\U00a0567-890";  //Actual Number :(932)324-1343
}

为什么要为电话号码添加不寻常的字符... 任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

这是一个“不间断的空间”(见http://unicode-table.com/en/00A0/)。

似乎这些符号可以通过某些外部同步出现在地址簿中。 但我在视觉上没有区别。

如果您在iPhone地址簿中输入数字,它将自动格式化为括号和数字之间的实际不可移动空格。在你的情况下情况并非如此。你根本没有空间。所以在代码中你有不间断的空格。