如何使用电话号码ios搜索地址簿

时间:2016-02-01 11:38:01

标签: ios objective-c nsmutablearray abaddressbook

我是IOS编程的新手。在我的简单应用程序中,我在NSMutableArray中存储名字,姓氏和电话号码。我有一个UITextfield,我在那里得到电话号码。我想根据手机号码搜索名字和姓氏。我正在存储名字,姓氏和电话不同的Mutable数组中的数字,以便如何搜索存储数组中的firstname和lastname。 这是我的代码

     ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    _firstname = [[NSMutableArray alloc]init];
    _lastname = [[NSMutableArray alloc]init];
    _number = [[NSMutableArray alloc]init];
    _fullname = [[NSMutableArray alloc]init];
    _arrContacts = [[NSMutableArray alloc]init];
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {                
            ABAddressBookRef addressBook = ABAddressBookCreate( );                
        });            
    }

    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        CFErrorRef *error = NULL;

        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

        CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

        for(int i = 0; i < numberOfPeople; i++) { 
            ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );                
            NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));                
            NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));                
            if (firstName != nil) {
                [_firstname addObject:firstName];    
            }
            else{
                [_firstname addObject:@"Not Found"];
            }
            if (lastName != nil) {
                [_lastname addObject:lastName];
            }
            else{
                [_lastname addObject:@"Not Found"];
            }


            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);                
            [[UIDevice currentDevice] name];                
            NSArray *numbers = (__bridge NSArray *)(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));                
            if (numbers != nil) {                    
                [_arrContacts addObject:numbers];                    
                NSLog(@"Numbers:%@",_arrContacts); 
            }




            for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {                    
                NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i); 
                _addressBookNum = [_addressBookNum stringByAppendingFormat: @":%@",phoneNumber];                    
            }                
        }

        // NSLog(@"AllNumber:%@",_addressBookNum);

    }

    else {            
        // Send an alert telling user to change privacy setting in settings app            
    }

    CFRelease(addressBookRef);
    NSLog(@"This is Name: %@",_firstname);
    NSLog(@"This is LastName: %@",_lastname);
    NSLog(@"Number: %@",_arrContacts);    
}

请提出一些建议吗

1 个答案:

答案 0 :(得分:1)

不是在不同的数组中保存名称的姓氏和数字,而是将其存储在带有字典的单个数组中。假设NSMutabelArray *详细信息 创建具有三个值名称lastname和number的字典,并将此dictoianry添加到数组中,  所以你很容易进一步使用