【问题标题】:Retrieve Phone number by using kABPersonPhoneProperty使用 kABPersonPhoneProperty 检索电话号码
【发布时间】:2013-08-02 03:37:37
【问题描述】:

我无法从下面的代码中检索手机号码,我尝试了很多东西但无法解决。请帮帮我

//下面这段代码是用来保存手机号码和电话号码的

        ABRecordRef newPerson = ABPersonCreate(); 

        ABMultiValueAddValueAndLabel(phone, (__bridge CFTypeRef)(txtMob2.text), kABHomeLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonPhoneProperty, phone, &error);

//image
        NSData *dataref = UIImagePNGRepresentation(imgProfile.image);
        ABPersonSetImageData(newPerson, (__bridge CFDataRef)(dataref), &error);

        ABAddressBookAddRecord(addressbook, newPerson, &error);
        ABAddressBookSave(addressbook, nil);
        CFRelease(newPerson);

// 现在下面的代码是显示细节。

 - (void)displayContacts
{
    int i;
    appDelegate.arr_contact = [[NSMutableArray alloc] init];

    ABAddressBookRef contactBook =ABAddressBookCreateWithOptions(nil, NULL);
    NSArray *allData = (__bridge_transfer NSArray *)(ABAddressBookCopyArrayOfAllPeople(contactBook));
    NSUInteger contactNum = 0;

    NSInteger recordId;
    ABRecordRef recId;

    for (i =0; i < [allData count]; i++)
    {
        appDelegate.dict_contact =[[NSMutableDictionary alloc] init];
        ABRecordRef ref =(__bridge ABRecordRef)(allData[i]);

        //mobile no and phone no
        ABMultiValueRef mobNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFIndex PhoneCount = ABMultiValueGetCount(mobNum);
        for (int k  = 0; k <PhoneCount; k++) {

           strMobile = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(mobNum, k));
           strPhone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(mobNum, k));

            if ([strMobile isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                mobileno = (__bridge NSString*)ABMultiValueCopyValueAtIndex(mobNum, k);
            }
            else if ([strPhone isEqualToString:(NSString *)kABHomeLabel])
            {
                phoneno = (__bridge NSString*)ABMultiValueCopyValueAtIndex(mobNum, k);
                break;
            }

            NSLog(@"Mobile: %@ phone: %@, %@",mobileno, phoneno, strPhone);
            [appDelegate.dict_contact setObject:[NSString stringWithFormat:@"%@",mobileno] forKey:@"mobno"];
            [appDelegate.dict_contact setObject:[NSString stringWithFormat:@"%@",phoneno] forKey:@"phnno"];
        }

        //image
        NSData *imgData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
        UIImage *image;
        if (imgData)
        {
           image = [UIImage imageWithData:imgData];
            NSLog(@"add image: %@",image);
        }else
        {
            image = [UIImage imageNamed:@"dummy.png"];
        }
        [appDelegate.dict_contact setObject:image forKey:@"image"];
        [appDelegate.arr_contact addObject:appDelegate.dict_contact];
    }
}

请检查我编辑的代码。

【问题讨论】:

    标签: iphone ios5 ios6 addressbook


    【解决方案1】:

    请查看此代码....

    -(void)GetAddressBook
    {
       Contacts = [[NSMutableArray alloc]init];
    
       if (ABAddressBookCreateWithOptions) {
    
       @try {
    
            ABAddressBookRef addressBook = ABAddressBookCreate();
            // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
            if (!addressBook) {
                NSLog(@"opening address book");
            }
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
            CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    
            NSLog(@"opening address book ==%ld",nPeople);
    
            for (int i=0;i < nPeople;i++) {
    
                NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
                ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
                NSString *Contact;
                ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
                CFStringRef firstName, lastName;
                NSMutableArray *array = [[NSMutableArray alloc]init];
                NSString *email;
                firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
                lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
                ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
                array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy];
                email = ([array count] > 0) ? array[0] : @"";
    
                if(firstName)
                {
                    Contact = [NSString stringWithFormat:@"%@", firstName];
                    if(lastName)
                        Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }
                [dOfPerson setObject:Contact forKey:@"name"];
                [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"];
                [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"];
                [dOfPerson setObject:email forKey:@"email"];
    
                NSString* mobileLabel;
                for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++)
                {
                    mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                    if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                    {
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    }
                    else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                    {
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                        break ;
                    }
                }
                [Contacts addObject:dOfPerson];
            }
        }
        @catch (NSException * e) {
            NSLog(@"Exception: %@", e);
    
    
        }
        dispatch_async(dispatch_get_main_queue(), ^{
    
    
    
        });
    

    在这里,您将获得电话号码、名字、姓氏和电子邮件 ID。这里的联系人NSMutableArray。另请查看此答案以获取完整的代码集。

    contact details from ipad using objective-c

    【讨论】:

    • 仍然如此。我只得到一个值,无论是手机号码还是固定电话号码,但我都插入了。还有检索图像的问题。 :(
    • @Abha 这不包括检索图像....您得到一个值,因为我已经包含 kABPersonPhoneProperty ...您也可以包含其他值:)
    • ABAddressBookRef addressBook = ABAddressBookCreate();在 ios6.0 中已弃用如果你使用它你不应该进入 iPhone 但你会进入模拟器 你必须使用这个 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    【解决方案2】:

    试试这个在这段代码中,如果你希望手机号码只使用这个 kABPersonPhoneProperty,我会同时获取姓名和手机号码:

    CFErrorRef * error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    for (int i=0;i < nPeople;i++)
    {
        NSString* name;
        NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
        CFStringRef firstName;
        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        name = (__bridge NSString *)(firstName);
        [tempContactDic setValue:name forKey:@"name"];
        //fetch email id
        NSString *strEmail;
        NSMutableString* strMobile;
        ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
        strEmail = (__bridge  NSString *)tempEmailref;
        strEmail = [strEmail stringByReplacingOccurrencesOfString:@"-" withString:@""];
        strEmail = [strEmail stringByReplacingOccurrencesOfString:@"(" withString:@""];
        strEmail = [strEmail stringByReplacingOccurrencesOfString:@")" withString:@""];
        strEmail = [strEmail stringByReplacingOccurrencesOfString:@" " withString:@""];
        strEmail = [strEmail stringByReplacingOccurrencesOfString:@"+" withString:@""];
        strMobile = [[NSMutableString alloc] initWithString:strEmail];
        if ([strEmail length] == 10) {
            [strMobile insertString:@"91" atIndex:0];
        }
        else {
    
        }
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterDecimalStyle];
        NSNumber * myNumber = [f numberFromString:strMobile];
        [tempContactDic setValue:myNumber forKey:@"phone"];
    
        [contactsArray addObject:tempContactDic];
    }
    /// With below two steps of code you'll get the contacts in alphabetical order
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    [contactsArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    

    【讨论】:

    • 感谢代码,但我的问题是我想要手机号码。以及电话号码,但我没有得到。我只得到一个值,手机号码和固定电话号码。当我插入数字时,它可以完美保存,但显示有问题。
    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多