2015-05-13 135 views
0

我需要訪問用戶地址簿並使用profile image和每個用戶的name填充表格視圖。從地址簿中顯示聯繫人

我收到警告'ABAddressBookCreate' is deprecated: first deprecated in iOS 6.0並且也沒有顯示聯繫人。

我迄今爲止代碼:

ABAddressBookRef m_addressbook = ABAddressBookCreate(); 



    if (!m_addressbook) { 

     NSLog(@"opening address book"); 

    } 



    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook); 

    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook); 



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

     NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary]; 



     ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 



     //For username and surname 

     ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge_transfer NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty)); 

     CFStringRef firstName, lastName; 

     firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 

     lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 

     [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"]; 



     //For Email ids 

     ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty); 

     if(ABMultiValueGetCount(eMail) < 0) { 

      [dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"]; 



     } 



     //For Phone number 

     NSString* mobileLabel; 

     for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) { 

      mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i); 

      if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) 

      { 

       [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"]; 

      } 

      else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) 

      { 

       [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"]; 

       break ; 

      } 



      [contactList addObject:dOfPerson]; 

      CFRelease(ref); 

      CFRelease(firstName); 

      CFRelease(lastName); 

     } 

     NSLog(@"array is %@",contactList); 

    } 
+1

http://stackoverflow.com/questions //取/ 22800634/abaddressbookcreate-deprecated? – Larme

+0

可能重複的[如何從通訊錄聯繫人(iphone sdk)]中獲取電話號碼](http://stackoverflow.com/questions/286207/how-to-get-a-phone-number-from-an- address-book-contact-iphone-sdk) –

回答

2

//請求授權給通訊簿

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); 

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { 
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { 
     if (granted) { 
      // First time access has been granted, add the contact 

      [self fetchFromAddressBook]; 
     } else { 
      // User denied access 
      // Display an alert telling user the contact could not be added 
     } 
    }); 
} 
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { 
    // The user has previously given access, add the contact 
    [self fetchFromAddressBook]; 
} 
else { 
    // The user has previously denied access 
    // Send an alert telling user to change privacy setting in settings app 
} 

從通訊錄

-(void)fetchFromAddressBook{ 


CFErrorRef *error = NULL; 
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); 
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook); 

NSInteger actual; 



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)); 
    ABRecordID recordID = ABRecordGetRecordID(person); 
    NSDate *birthday = (__bridge NSDate *)(ABRecordCopyValue(person, kABPersonBirthdayProperty)); 
    NSData *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize); 



ABMultiValueRef phoneNumbers = ABRecordCopyValue((ABRecordRef)(person), kABPersonPhoneProperty); 

NSString* [email protected]""; 
for (int i=0; i < ABMultiValueGetCount(phoneNumbers); i++) { 

    mobile = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i); 

    NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"]; 
    mobile = [[mobile componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""]; 
    mobile= [mobile stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 

    mobile=[mobile stringByReplacingOccurrencesOfString:@" " withString:@""]; 


} 

    NSLog(@"Name:%@ %@", firstName, birthday,mobile); 

} 



} 
+0

我怎樣才能得到用戶的電話號碼? – Illep

+0

請看編輯答案 – Mukesh

+0

所以我不能得到名字,姓氏和電話號碼togeather。你的方式,我不知道如何映射名稱和他們的數字。幫助 – Illep

相關問題