如何从iOS中的地址簿获取家庭和工作电话号码

问题描述 投票:1回答:1

我正在尝试从地址簿获取电话号码,我可以获得手机,iPhone和主要电话号码,但我无法带回家工作。我在这里检查了很多答案,但我仍然无法解决这个问题。这是我的代码。

  (BOOL)askContactsPermission {

  if (ABAddressBookRequestAccessWithCompletion) { // if in iOS 6

    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
                    });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact

    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
}
else{ // if not in iOS 6
    // just get the contacts directly
}
}   

 -(void)parseAdressBook{

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople( addressBook );



allContacts=[[NSArray alloc]init];

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

ABRecordRef *person;

for (CFIndex k = 0; k < ABAddressBookGetPersonCount( addressBook ); k++)
{


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

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

    NSLog(@"%lu first Name=%@",k,firstName);
    NSLog(@"%lu last name=%@",k,lastName);
    ABRecordRef aSource = CFArrayGetValueAtIndex(allSources,k);
    ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty));


    NSString* mobileLabel;
    NSString* basic_mobile;
    NSString* work_mobile;
    NSString* home_mobile;
    NSString* strOtherMobile;

    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {



        mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            home_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i); //Mobile
            NSLog(@"%ld,home mobile=%@",k,home_mobile);


        }

        if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            basic_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i); //iPhone
            NSLog(@"basic_mobile=%@",basic_mobile);

        }
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
        {
            work_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);  ////Main
            NSLog(@"work_mobile=%@",work_mobile);
        }



        if([mobileLabel isEqualToString:(NSString *)kABHomeLabel])
        {
            strOtherMobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"strOtherMobile=%@",strOtherMobile);
        }

        if([mobileLabel isEqualToString:(NSString *)kABWorkLabel])
        {
            strOtherMobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"strOtherMobile=%@",strOtherMobile);
        }


    }
  }
}

顺便问一下strOtherMobile = null

ios objective-c
1个回答
0
投票
if([mobileLabel isEqualToString:(NSString *)kABHomeLabel])
    {
        work_mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"strOtherMobile=%@",strOtherMobile);
    }

纠正关于strOtherMobile = null的上述错误

if([mobileLabel isEqualToString:(NSString *)kABHomeLabel])
    {
        strOtherMobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"strOtherMobile=%@",strOtherMobile);
    }

而不是work_mobile使用strOtherMobile

© www.soinside.com 2019 - 2024. All rights reserved.