0

i m using following code for Backup Full Address Book.

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    CFArrayRef peopleForBackup  = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(peopleForBackup);
    NSString *vcardString = [[NSString alloc] initWithData:(NSData *)CFBridgingRelease(vcards) encoding:NSUTF8StringEncoding];

in Above code i can not get Notes from Contacts.

Please help me, how can i take a full address book as a Backup..?

Brijesh
  • 171
  • 6

1 Answers1

0

get Complete Address Book ,

   -(void)getAddressbookContacts
    {
        //Get Contacts From PhoneBook Contacts
        NSString *fullName;
        UIImage  *img;
        
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);
        NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
        NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
        
        for (NSInteger i = 0; i < numberOfPeople; i++) {
            ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
            
            // get phone numbers
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            NSString* mobile=@"";
            NSString* mobileLabel;
            
            CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
            for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
            {
                mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
    //            NSLog(@"mobileLabel=%@",mobileLabel);
    
                //Only add numbers who have saved in Mobile or IPhone (eg.Home,Work,..)
                if([mobileLabel isEqualToString:(__bridge NSString *)kABPersonPhoneMobileLabel] || [mobileLabel isEqualToString:(__bridge NSString *)kABPersonPhoneIPhoneLabel])
                {
                    NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
                    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
                    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
                    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
                    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
                    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    //                phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
    //                NSLog(@"phoneNumber=%@",phoneNumber);
                    
                    // contact number should be mobile number or iphone
                    mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
                    NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    //                ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty);
                    
                    //Fetch Email Address of Contact
    //                NSArray *emailAddresses = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
                    //NSLog(@"email is %@",emailAddresses);
                    
                    //Fetch Image of Contact
                    NSData  *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
                    
                    if(lastName == nil)
                    {
                        fullName = firstName;
                    }
                    else
                    {
                        fullName = [firstName stringByAppendingString:@" "];
                        fullName = [fullName stringByAppendingString:lastName];
                    }
                    
                    if(imgData == nil)
                    {
                        //NSLog(@"no image found");
                        img = [UIImage imageNamed:@"contact"];
                    }
                    else
                    {
                        img = [UIImage imageWithData:imgData];
                    }
                    
                    NSMutableDictionary  *dictContactDetails = [[NSMutableDictionary alloc]init];
                    [dictContactDetails setValue:fullName forKey:@"full_name"];
                    [dictContactDetails setValue:img forKey:@"profile_image"];
                    [dictContactDetails setValue:mobile forKey:@"mobile_number"];
                    [dictContactDetails setValue:phoneNumber forKey:@"number"];
                    
                    //Add Fullname,Image & number into Array & Display it into Table
                    [arrayOfInviteContacts addObject:dictContactDetails];
                }
            }
            CFRelease(phoneNumbers);
        }
        
        NSLog(@"arrayOfInviteContacts=%lu, %@",(unsigned long)arrayOfInviteContacts.count,arrayOfInviteContacts);
        
        [tblInviteFriends reloadData];
    }
Community
  • 1
  • 1
Mehul
  • 3,033
  • 23
  • 37
  • is it possible to take a backup full contact instead define each field..? in above code i need to add each and every field and add that. – Brijesh Aug 17 '15 at 09:48
  • @iCoderz, you have to check if its any field is added then you can get it. an yes you have to add that one. one by one. – Mehul Aug 17 '15 at 10:12
  • In My code i got all Fields instead of "Notes". any other way to get "Notes" in above my code..?. From your code i need to add all fields one bye one. and create Loop and it's take time. i don't want it. – Brijesh Aug 17 '15 at 12:43
  • @iCoderz, let me try . give some time. will inform you – Mehul Aug 17 '15 at 13:08
  • NSString *notes = (__bridge_transfer NSString*)ABRecordCopyValue(record, kABPersonNoteProperty); – Mehul Aug 17 '15 at 13:53