联系提取未在ios中显示第一次

时间:2016-02-03 12:49:15

标签: ios synchronization contacts nsfetchedresultscontroller xmppframework

我正在开发一个聊天应用程序,需要联系人进行聊天,但是当第一次没有显示联系人时,重新启动应用程序获取然后显示这里是我的代码

-(NSMutableArray *)getAllContacts {

sleep(6);
// The user has previously given access, add the contact

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBookRef);
CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, source, kABPersonSortByFirstName));
CFIndex nPeople = ABAddressBookGetPersonCount(addressBookRef);
NSMutableArray* items = [[NSMutableArray alloc] init];

if (!allPeople || !nPeople) {
    NSLog(@"people nil");
}


CFStringRef description = CFCopyDescription(allPeople);
NSLog(@"Array %@", description);
CFRelease(description);

for (int i = 0; i < nPeople; i++) {

             //        @autoreleasepool {

        //data model
        ContactsData *contacts = [ContactsData new];

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //get First Name
        CFStringRef firstName = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty);
        contacts.firstNames = (__bridge NSString*)firstName;

        if (firstName != NULL) {
            CFRelease(firstName);
        }

        //get Last Name
        CFStringRef lastName = (CFStringRef)ABRecordCopyValue(person,kABPersonLastNameProperty);
        contacts.lastNames = (__bridge NSString*)lastName;

        if (lastName != NULL) {
            CFRelease(lastName);
        }


        if (!contacts.firstNames) {
            contacts.firstNames = @"";
        }

        if (!contacts.lastNames) {
            contacts.lastNames = @"";
        }



        contacts.contactId = ABRecordGetRecordID(person);
        //append first name and last name
        contacts.fullname = [NSString stringWithFormat:@"%@ %@", contacts.firstNames, contacts.lastNames];


        // get contacts picture, if pic doesn't exists, show standart one
        CFDataRef imgData = ABPersonCopyImageData(person);
        NSData *imageData = (__bridge NSData *)imgData;
        contacts.image = [UIImage imageWithData:imageData];

        if (imgData != NULL) {
            CFRelease(imgData);
        }

        if (!contacts.image) {
            contacts.image = [UIImage imageNamed:@"avatar.png"];
        }


        //get Phone Numbers
        NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
        ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);

        for(CFIndex i=0; i<ABMultiValueGetCount(multiPhones); i++) {
            @autoreleasepool {
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                NSString *phoneNumber = CFBridgingRelease(phoneNumberRef);
                 phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
                if (phoneNumber != nil)[phoneNumbers addObject:phoneNumber];
                //NSLog(@"All numbers %@", phoneNumbers);
            }
        }

        if (multiPhones != NULL) {
            CFRelease(multiPhones);
        }

        [contacts setNumbers:phoneNumbers];

        //get Contact email
        NSMutableArray *contactEmails = [NSMutableArray new];
        ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

        for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
            @autoreleasepool {
                CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
                NSString *contactEmail = CFBridgingRelease(contactEmailRef);
                if (contactEmail != nil)[contactEmails addObject:contactEmail];
                // NSLog(@"All emails are:%@", contactEmails);
            }
        }

        if (multiPhones != NULL) {
            CFRelease(multiEmails);
        }

        [contacts setEmails:contactEmails];

        [items addObject:contacts];

        #ifdef DEBUG
        NSLog(@"Person is: %@", contacts.firstNames);
        NSLog(@"Phones are: %@", contacts.numbers);
        NSLog(@"Email is:%@", contacts.emails);
        #endif

         }
        //    } //autoreleasepool
        CFRelease(allPeople);
         CFRelease(addressBookRef);
         CFRelease(source);


          return items;

                 }

现在我正在联系,现在我从

获取联系人
- (NSFetchedResultsController *)fetchedResultsController
 {
if (fetchedResultsController == nil)
{
    NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                              inManagedObjectContext:moc];

    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
    NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

    NSArray *sortDescriptors = @[sd1, sd2];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];

    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setFetchBatchSize:20];

    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                   managedObjectContext:moc
                                                                     sectionNameKeyPath:@"sectionNum"
                                                                              cacheName:nil];
    [fetchedResultsController setDelegate:self];


    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error])
    {
        DDLogError(@"Error performing fetch: %@", error);
        NSLog(@"Error performing fetch");
    }

}

return fetchedResultsController;

}

现在将此获取的控制器分配给数组,如

self.UserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];

但无法将联系人提取到我想念的tableview,请帮帮我

0 个答案:

没有答案