0

我无法从 iPhone 通讯录中获取电话号码。

当号码不包含 +45 之类的国家代码前缀时没有问题,但如果包含,我的应用程序会崩溃......

这是一个已知的问题?我一直没能找到任何关于它的...

谢谢

编辑:

我得到这样的电话号码:

    -(void)getContact 
    {

        ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] init];
        pp.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
        pp.peoplePickerDelegate = self;
        [self presentModalViewController:pp animated:YES];
        [pp release];


    }

    - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
        // assigning control back to the main controller
        [self dismissModalViewControllerAnimated:YES];
    }

    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
        return YES;
    }

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

            ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
            saveString = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);
            saveString = [saveString stringByReplacingOccurrencesOfString:@" " withString:@""];
            nummerTextField.text = saveString;
        }
4

2 回答 2

0

您如何检索您的通讯簿对象,一旦检索到它,您如何处理它以从中获取号码。我正在使用下面显示的代码来执行您提到的相同操作,并且它可以准确地获取号码。

ABRecordRef person = ABAddressBookGetPersonWithRecordID(appDelegate.addressBook, contactId);

ABMultiValueRef multiValue = ABRecordCopyValue(person, kABPersonPhoneProperty);

NSArray *allNumbers = (NSArray *)ABMultiValueCopyArrayOfAllValues(multiValue);
NSMutableDictionary *filteredNumbers = [NSMutableDictionary new];

if([allNumbers count] > 0) {
    for(int contactIndex = 0; contactIndex < [allNumbers count]; contactIndex++) {
        NSString *contactValue = (NSString *)ABMultiValueCopyLabelAtIndex(multiValue, contactIndex);
        if(!([contactValue isEqualToString:@"_$!<WorkFAX>!$_"] || [contactValue isEqualToString:@"_$!<HomeFAX>!$_"] || [contactValue isEqualToString:@"_$!<Pager>!$_"])) {

            if([[contactValue substringWithRange:contactLabelCharacterCustom] isEqualToString:@"_$"])
                typeOfContact = [contactValue substringWithRange:contactLabelCharacter];
            else
                typeOfContact = [contactValue substringWithRange:(NSRange){0,1}];
            NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, contactIndex);
            [filteredNumbers setValue:typeOfContact forKey:value];
            [value release];
            value = nil;
        }
        [contactValue release];
        contactValue = nil;
    }
}

我相信它会帮助你。

干杯

于 2011-01-17T10:33:56.313 回答
0

这解决了我的问题。希望有人觉得它有帮助。

ABMultiValueRef multiValue = ABRecordCopyValue(person, property);

        NSString *number = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, ABMultiValueGetIndexForIdentifier(multiValue, identifier));

// Error was here: NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, contactIndex);

        //Copy the number etc before cleaning everything up

        saveString = number;
        saveString = [saveString stringByReplacingOccurrencesOfString:@" " withString:@""];
        nummerTextField.text = saveString;

        [number release];
        CFRelease(multiValue);
于 2011-01-17T15:46:57.260 回答