草庐IT

iphone - IOS AddressBook 没有从联系人列表中获取所有电话号码

coder 2023-09-25 原文

我在我的 IOS 应用程序中使用 Addressbook 来获取联系人姓名和号码,但奇怪的是它显示了一些联系人的电话号码。

我希望它能显示所有联系人列表和电话号码。

下面是我获取电话号码的代码:

-(void)getAddressbookData
{
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
        ABAddressBookRef addressBook = ABAddressBookCreate();
    #else
        CFErrorRef *error = nil;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    #endif
        NSArray * people;
        BOOL accessGranted = [self __addressBookAccessStatus:addressBook];

        if (accessGranted)
        {
            people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
            // Do whatever you need with thePeople...
        }
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray *contactArray = [[NSMutableArray alloc] init];

        for (CFIndex i = 0; i < nPeople; i++)
        {
            ABRecordRef record = CFArrayGetValueAtIndex((__bridge CFArrayRef)(people), i);
            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
            NSString *fullName = nil;

            if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst)
                fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            else
                fullName = [NSString stringWithFormat:@"%@, %@", lastName, firstName];

            [contactArray addObject:fullName];

            //
            // Phone Numbers
            //
            ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
            CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );

            NSMutableArray *numbersArray = [[NSMutableArray alloc] init];
            for ( CFIndex k=0; k<phoneNumberCount; k++ )
            {
                CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );
                // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"

                // Find the ones you want here
                //
               // NSLog(@"-----PHONE ENTRY -> name:%@ :  %@ : %@", fullName, phoneNumberLocalizedLabel, phoneNumberValue );
                [numbersArray addObject:CFBridgingRelease(phoneNumberValue)];

                CFRelease(phoneNumberLocalizedLabel);
                CFRelease(phoneNumberLabel);
                CFRelease(phoneNumberValue);
            }

           // NSLog(@"phone numbers %@", numbersArray);
            [contactDictionary setObject:numbersArray forKey:fullName];

            CFRelease(record);

        }

        selectContacts = contactArray;
       // NSLog(@"dictionary of array %@", contactDictionary);

        //NSLog(@"contacts count %d", [selectContacts count]);
    }

    -(BOOL)__addressBookAccessStatus:(ABAddressBookRef) addressBook
    {
        __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);
            // dispatch_release(sema);
        }
        else { // we're on iOS 5 or older
            accessGranted = YES;
        }
        return accessGranted;
    }

所以一些联系人的 numbersArray 是空白的,我不知道为什么会这样。

最佳答案

试试这个电话号码。

    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(id person in people){
    //fetch multiple phone nos.
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j);
            [numbersArray addObject:phone];
            [phone release];
        }
    }

并且您必须在使用前分配您的数组。在 viewDidLoad 方法中为分配数组写这个

           numbersArray=[[NSMutableArray alloc] init];

关于iphone - IOS AddressBook 没有从联系人列表中获取所有电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607229/

有关iphone - IOS AddressBook 没有从联系人列表中获取所有电话号码的更多相关文章

  1. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  7. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

  8. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  9. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  10. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

随机推荐