如何从阵列中删除facebook联系人? Ios地址簿

时间:2014-10-09 13:06:30

标签: ios nsmutablearray nsarray addressbook

我使用的是地址簿,但不希望他带来facebook的联系方式。我做了一个if语句来过滤,但是我没有从我的数组中删除它。

ABMultiValueRef emails  = ABRecordCopyValue(person, kABPersonEmailProperty);

    CFIndex numberOfEmails = ABMultiValueGetCount(emails);
    userDetail.allEmails = [@[] mutableCopy];
    for (CFIndex i = 0; i < numberOfEmails; i++) {

        NSString *email = CFBridgingRelease(ABMultiValueCopyValueAtIndex(emails, i));

        if ([email containsString:@"@facebook.com"]) {
            NSLog(@"remove emails: %@", userDetail.allEmails);

        }
        [userDetail.allEmails addObject:email];
    }

1 个答案:

答案 0 :(得分:0)


我使用NSPredicate&amp; NSCompoundPredicate进行过滤。

 CFErrorRef *error = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray *mylist = [[NSMutableArray alloc] initWithCapacity:numberOfPeople];
        for(int i = 0; i < numberOfPeople; i++) {
            ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
            NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
            NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
            ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
            for (CFIndex i = 0; i < ABMultiValueGetCount(email); i++) {
                NSString *emailId = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(email, i);
                NSDictionary *myObjlist = @{@"email":emailId,@"name":[NSString stringWithFormat:@"%@ %@",firstName,lastName]};
                //NSLog(@"Name:%@ %@ : %@", firstName, lastName,myObjlist);
                [mylist addObject:myObjlist];

            }
        }
        //NSLog(@"mylist : %@",mylist);
        NSPredicate *p = [NSPredicate predicateWithFormat:
            @"SELF['email'] CONTAINS '@facebook.com'"];
        NSPredicate *notFilter = [NSCompoundPredicate notPredicateWithSubpredicate:p];
        NSArray *res = [mylist filteredArrayUsingPredicate:notFilter];
        NSLog(@"awesome %@", res);

如果您有任何疑问,请与我联系。