仅获取使用相同应用的手机通讯录

时间:2015-11-16 11:06:56

标签: ios objective-c contacts

我正在开发一个应用程序,我想从iphone中的联系人列表中获取正在使用相同应用程序的联系人。

怎么做?任何示例代码或链接?

请帮帮我。

  

注意:我不想在我的ios应用程序中获取所有联系人,我   只想获取使用相同应用程序的联系人。

2 个答案:

答案 0 :(得分:0)

首先导入AddressBook框架

然后调用这两个函数

-(void)AddressBookFetch{
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
        // if you got here, user had previously denied/revoked permission for your
        // app to access the contacts, and all you can do is handle this gracefully,
        // perhaps telling the user that they have to go to settings to grant access
        // to contacts
        [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (!addressBook) {
        NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
        return;
    }

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (error) {
                NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
            }

            if (granted) {
                // if they gave you permission, then just carry on

                [self listPeopleInAddressBook:addressBook];
            } else {
                // however, if they didn't give you permission, handle it gracefully, for example...

                dispatch_async(dispatch_get_main_queue(), ^{
                    // BTW, this is not on the main thread, so dispatch UI updates back to the main queue

                    [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                });
            }
        CFRelease(addressBook);
    });

}

然后按此功能列出联系人

- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
    NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    numberOfPeople = [allPeople count];

    for (NSInteger i = 0; i < numberOfPeople; i++) {
        ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);
        if (firstName==nil) {
            firstName=@"";
        }

        [nm addObject:firstName];
        if (lastName==nil) {
            lastName=@"";
        }
        [ttl addObject:lastName];


        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

        // CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
        //for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));
        NSLog(@"  phone:%@", phoneNumber);
        if (phoneNumber==nil) {
            phoneNumber=@"";
        }
        [phn addObject:phoneNumber];
        // }
        // CFRelease(phoneNumbers);

        NSLog(@"=============================================");
    }
}

答案 1 :(得分:0)

没有后端是不可能的,你不能只在ios应用程序中做到。