How to get phone number from address book as string?

前端 未结 4 1044
耶瑟儿~
耶瑟儿~ 2021-02-06 16:59

I have a text field with a contact name and I want to get the phone number for that:

ABAddressBookRef adressBook = ABAddressBookCreate();

NSArray *people = (NSA         


        
相关标签:
4条回答
  • 2021-02-06 17:29
    CFStringRef phoneRef = ABRecordCopyValue(person, kABPersonPhoneProperty) ;
    NSString *phoneNumber = (NSString *) phoneRef;
    
    0 讨论(0)
  • 2021-02-06 17:36
    NSMutableArray *phoneNumbers = [[[NSMutableArray alloc] init] autorelease];
    ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty);
    for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);++i) {
        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
        NSString *phoneNumber = (NSString *) phoneNumberRef;
    
        [phoneNumbers addObject:phoneNumber];
    }
    
    0 讨论(0)
  • 2021-02-06 17:39

    kABPersonPhoneProperty is a multi-value property, but you're treating it as a string. You should get the individual numbers and print them separately.

    0 讨论(0)
  • 2021-02-06 17:44

    For those searching for phone extraction. You can extract the phone numbers from a text and then replace it with @"", for example:

    NSString *userBody = @"This is a text with 30612312232 my phone";
    if (userBody != nil) {
        NSError *error = NULL;
        NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
        NSArray *matches = [detector matchesInString:userBody options:0 range:NSMakeRange(0, [userBody length])];
        if (matches != nil) {
            for (NSTextCheckingResult *match in matches) {
                if ([match resultType] == NSTextCheckingTypePhoneNumber) {
                    DbgLog(@"Found phone number %@", [match phoneNumber]);
                }
            }
        }
    }
    

    `

    0 讨论(0)
提交回复
热议问题