1

電話帳へのアクセスが必要な Xcode で iPhone アプリを実行しています。アドレス帳のすべての電話番号が必要で、NSArray.

4

3 に答える 3

0

このコードは Xcode 4.5.1>=0 用です。バージョンより下の場合は、if 条件を記述する必要はありません。ABAddressBookCreate()アドレス帳に割り当てるだけです。

   __block BOOL  accessGranted = NO;
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
            NSLog(@"Device version is greater than 6.0");
            addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
                if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
                      dispatch_semaphore_t sema = dispatch_semaphore_create(0);

                        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                            accessGranted = granted;
                            dispatch_semaphore_signal(sema);
                        });

                        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
                    }
        }
        else{
            addressBook = ABAddressBookCreate();
            accessGranted = YES;
        }

 if (accessGranted) {

     NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
     CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);

     for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
        ABRecordRef person = CFArrayGetValueAtIndex(people, i);

        NSString *name = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

        ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty);
        NSMutableArray *individualPhoneNumbers = [[NSMutableArray alloc] init];
        if (ABMultiValueGetCount(multiPhones) >0) {

            for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);++i) {
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;

               [individualPhoneNumbers addObject:phoneNumber];
            }
           [phoneNumbers addObject:individualPhoneNumbers];
        }

   }
于 2012-12-24T07:31:00.560 に答える
0
Try this,
         NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];

     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];
   }
于 2012-12-24T07:03:45.143 に答える