Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当新联系人添加到iPhone上的通讯簿时,它的名字属性NSLogs为null_Ios_Iphone_Objective C_Null_Abaddressbook - Fatal编程技术网

Ios 当新联系人添加到iPhone上的通讯簿时,它的名字属性NSLogs为null

Ios 当新联系人添加到iPhone上的通讯簿时,它的名字属性NSLogs为null,ios,iphone,objective-c,null,abaddressbook,Ios,Iphone,Objective C,Null,Abaddressbook,我有一个正在访问通讯簿的应用程序。该应用程序通过编程方式访问viewDidLoad上的通讯簿,提取所有联系人,并将他们的名字放入名为firstName的NSString对象中 出于某种原因,如果我停止使用该应用程序,在通讯簿中添加一个新联系人,然后再次开始运行该应用程序,则新联系人的名字将显示为空 我查看了所有正在工作的联系人,并将它们与我添加的新联系人进行了比较,没有什么不同。他们都有手机号码和名字 但无论我添加了多少新联系人,他们的所有名字都会以null打印到控制台上 以下是我正在使用的代码

我有一个正在访问通讯簿的应用程序。该应用程序通过编程方式访问viewDidLoad上的通讯簿,提取所有联系人,并将他们的名字放入名为firstName的NSString对象中

出于某种原因,如果我停止使用该应用程序,在通讯簿中添加一个新联系人,然后再次开始运行该应用程序,则新联系人的名字将显示为空

我查看了所有正在工作的联系人,并将它们与我添加的新联系人进行了比较,没有什么不同。他们都有手机号码和名字

但无论我添加了多少新联系人,他们的所有名字都会以null打印到控制台上

以下是我正在使用的代码:

ABAddressBookRef m_addressbook =  ABAddressBookCreateWithOptions(NULL, NULL);


ABAddressBookCopyArrayOfAllPeople(m_addressbook);


__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @autoreleasepool {
            // Write your code here...
            // Fetch data from SQLite DB
        }
    });

    ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error)

    {
        accessGranted = granted;

        NSLog(@"Has access been granted?: %hhd", accessGranted);
        NSLog(@"Has there been an error? %@", error);


        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
    accessGranted = YES;


}

if (accessGranted) {


    NSArray *allContacts = (__bridge_transfer NSArray
                            *)ABAddressBookCopyArrayOfAllPeople(m_addressbook);


    int z = allContacts.count - 1;


    for (int i = 0; i < z; i++)


    {

        NSArray *allContacts = (__bridge_transfer NSArray
                                *)ABAddressBookCopyArrayOfAllPeople(m_addressbook);

        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];



        NSString *firstName = (__bridge_transfer NSString
                               *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);


        NSLog(@"FIRSTNAME: %@", firstName);
到了最后一句话,NSLog@FIRSTNAME:%@,名字;,除了刚刚添加的新姓名外,所有联系人的名字都打印正确


我甚至运行了一个测试,删除了一个正常工作的联系人,然后完全按照原来的样子重新添加了他们,并且可以肯定的是,他们现在正在使用一个空的名字值进行登录。

试试这个功能这对我来说是有效的

 -(void)collectAddressBookContacts 
{
contactList = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)
{
    NSMutableDictionary *aPersonDict = [[NSMutableDictionary alloc] init];
    ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
    NSString *fullName = (__bridge NSString *) ABRecordCopyCompositeName(ref);
    if (fullName) {
        [aPersonDict setObject:fullName forKey:@"fullName"];


        // collect phone numbers

        ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        NSString *phoneNumber=@"";
        for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
        {

            if ([phoneNumber isEqualToString:@""])
            {
                phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, j);
            }
            else
            {
                NSString *secondnumber=(__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, j);
                phoneNumber=[NSString stringWithFormat:@"%@,%@",phoneNumber,secondnumber];
            }

        }
        [aPersonDict setObject:phoneNumber forKey:@"phoneNumbers"];


        ABMultiValueRef Address = ABRecordCopyValue(ref, kABPersonAddressProperty);
        NSString *address=@"";

        for(CFIndex j = 0; j < ABMultiValueGetCount(Address); j++)
        {
            address = (__bridge NSString *) ABMultiValueCopyValueAtIndex(Address, j);

            NSMutableDictionary *dictaddress=(NSMutableDictionary *)address;
            address=[NSString stringWithFormat:@"%@,%@,%@",[dictaddress objectForKey:@"Street"],[dictaddress objectForKey:@"City"],[dictaddress objectForKey:@"Country"]];
            break;
        }


        [aPersonDict setObject:address forKey:@"address"];

        // collect emails - key "emails" will contain an array of email addresses
        ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);

        NSString *email=@"";
        for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++)
        {

            if ([email isEqualToString:@""])
            {
                email =  (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, idx);
            }
            else
            {
                email=[NSString stringWithFormat:@"%@,%@",email,(__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, idx)];
            }
        }
        [aPersonDict setObject:email forKey:@"emails"];
        // if you want to collect any other info that's stored in the address book, it follows the same pattern.
        // you just need the right kABPerson.... property.


        [aPersonDict setObject:[UserDefaults objectForKey:@"userid"] forKey:@"userid"];
        [contactList addObject:aPersonDict];
    } else {
        // Note: I have a few entries in my phone that don't have a name set
        // Example one could have just an email address in their address book.
    }
}



}

我希望这段代码对您有用。

我终于找到了答案。出于某种原因,当我在iOS7中添加新联系人时,他们会像这样记录:555\U00a0975-5575,而不是像这样记录:555975-5575


我所要做的就是使用stringByReplacingOccurrencesOfString并删除多余的\U00a0。在做了一些研究之后,这似乎是苹果在使用iOS7添加电话号码时添加到电话号码上的额外空间。

您只需要在知道自己已经添加了地址簿引用和abrecordref之后,再创建地址簿引用和abrecordrefpermission@user3344977检查我的答案。Darshan你能给我们一个ContactData模型类吗?