2

以下のコードから携帯電話番号を取得できません。多くのことを試していますが、解決できません。私を助けてください

//以下のコードは、携帯電話番号と電話番号を保存するためのものです

        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];
    }
}

編集したコードを確認してください。

4

2 に答える 2

-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. コードの完全なセットについては、この回答もご覧ください。

Objective-C を使用した iPad からの連絡先の詳細

于 2013-08-01T09:34:45.380 に答える