2015-08-17 30 views
0

我使用以下代码备份完整地址簿。笔记没有得到备份完整地址簿从IOS

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]; 

以上代码我无法从联系人获取Notes。

请帮助我,我怎样才能把一个完整的地址簿作为备份..?

回答

0

得到完整的地址簿,

-(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* [email protected]""; 
      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]; 
    } 
+0

是有可能采取备份的充分接触,而不是定义各个字段的..?在上面的代码中,我需要添加每个字段并添加它。 – iCoderz

+0

@iCoderz,你必须检查它的任何字段是否被添加,然后你可以得到它。是的,你必须添加一个。逐个。 – Mehul

+0

在我的代码中,我得到了所有的字段而不是“注释”。任何其他方式来获得我的代码上面的“注释”..?。从你的代码我需要添加所有字段再见一个。并创建Loop,这需要时间。我不想要它。 – iCoderz