2013-05-24 5 views
12

Ich bin neu mit dem Adressbuch und ich würde versuchen, einige Informationen für die Kontakte im Telefon zu bekommen. Ist es möglich, den Bildkontakt aller Kontakte zu bekommen? Dies wird für iOS4 oder höher sein.Erhalten Bildkontakt im Adressbuch auf iOS

Vielen Dank im Voraus!

Antwort

15

Ja, es durchaus möglich ist, fragen Sie einfach jede Person Datensatz, ob es sich um ein Bild hat:

der Dokumentation Siehe: http://developer.apple.com/library/ios/#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/doc/uid/TP40007210

Relevante Funktionen:

ABPersonHasImageData(person) 
ABPersonCopyImageDataWithFormat(person) 

Die ABPersonCopyImageDataWithFormat a zurück CFDataRef. Konvertieren es NSData mit diesem (ARC) NSData* data = (__bridge_transfer NSData*) cfData;

Ein Bild kann dann mit [UIImage imageWithData:data]

+0

Danke für Ihre Antwort! Es wird mir sehr helfen! – lightless07

8

erstellt werden Das ist wirklich schön Code, fand ich all den Kontakt inaformation auszuwählen und vielleicht hilfreich für zukünftige Benutzer ..

AddressBookUI und Adressbuch Rahmen in Projekt

als in .h-Datei hinzufügen

#import <UIKit/UIKit.h> 
#import <AddressBookUI/AddressBookUI.h> 

@interface ViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate,UIAlertViewDelegate,UITextFieldDelegate,UINavigationControllerDelegate> 
{ 
    NSString * toEmail; 
} 

- (IBAction)getCaontactDetail:(id)sender; 

@end 

Und in .m Datei

- (IBAction)getCaontactDetail:(id)sender { 
    //open contact book 
    ABPeoplePickerNavigationController *addressBookController = [[ABPeoplePickerNavigationController alloc]init]; 
    addressBookController.peoplePickerDelegate = self; 
    addressBookController.delegate = self; 

    addressBookController.navigationBar.tintColor = [UIColor redColor]; 
    addressBookController.searchDisplayController.searchBar.tintColor = [UIColor redColor]; 
    addressBookController.searchDisplayController.searchBar.backgroundColor = [UIColor blackColor]; 

    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){ 
     [self presentViewController:addressBookController animated:YES completion:nil]; 
    } else { 
     [self presentModalViewController:addressBookController animated:YES]; 
    } 

    addressBookController = nil; 
} 

#pragma mark - 
#pragma mark ABPeoplePickerNavigationController Delegate Method 
// called when address book closed 
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker 
{ 
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){ 
     [peoplePicker dismissViewControllerAnimated:YES completion:nil]; 
    } else { 
     [peoplePicker dismissModalViewControllerAnimated:YES]; 
    } 
} 

// called when select any contact from address book 
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    //parse all data of selected contact 
    ABMultiValueRef addresses = (__bridge ABMultiValueRef)((__bridge NSMutableDictionary *)ABRecordCopyValue(person, kABPersonAddressProperty)); 
    NSArray *addressesArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(addresses); 
    NSDictionary *dictAddress = [addressesArray objectAtIndex:0]; 

    //get the phone number 
    ABMultiValueRef phone = (__bridge ABMultiValueRef)((__bridge NSMutableDictionary *)ABRecordCopyValue(person, kABPersonPhoneProperty)); 
    NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phone); 
    NSMutableString *strPhone = [NSMutableString string]; 

    for (int i=0; i<[phoneArray count]; i++) 
    { 
     [strPhone appendString:[NSString stringWithFormat:@"%@,",[phoneArray objectAtIndex:i]]]; 
    } 

    //convert in to NSString and NSInteger 
    NSInteger contactId = (NSInteger) ABRecordGetRecordID(person); 
    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty)[email protected]"":(__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)[email protected]"":(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
    NSString *street = [dictAddress valueForKey:@"Street"][email protected]"":[dictAddress valueForKey:@"Street"]; 
    NSString *telephone = [strPhone stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 
    NSString *city = [dictAddress valueForKey:@"City"][email protected]"":[dictAddress valueForKey:@"City"]; 
    NSString *postcode = [dictAddress valueForKey:@"ZIP"][email protected]"":[dictAddress valueForKey:@"ZIP"]; 
    NSString *state = [dictAddress valueForKey:@"State"][email protected]"":[dictAddress valueForKey:@"State"]; 
    NSString *country = [dictAddress valueForKey:@"CountryCode"][email protected]"":[dictAddress valueForKey:@"CountryCode"]; 


    NSString *recipientName = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; 

    NSLog(@"contactId : %i recipientName : %@ street : %@ telephone : %@ city : %@ postcode : %@ state : %@ country : %@",contactId,recipientName,street,telephone,city,postcode,state,country); 

//get the contact image 
    UIImage *img ; 
    if (person != nil && ABPersonHasImageData(person)) { 
     if (&ABPersonCopyImageDataWithFormat != nil) { 
      // iOS >= 4.1 
      img= [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)]; 
     } else { 
      // iOS < 4.1 
      img= [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)]; 
     } 
    } else { 
     img= nil; 
    } 
    contactIV.image = img; 
    contactNameLbl.text = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; 
    contactNumLbl.text = telephone; 


    //get email address 
    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 
    NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emails); 

    //display alert if contact does not contain email addreess 
    if (emailArray.count == 0) { 
     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"The person you selected does not have an email address on file. Please enter their email address below." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Cancel",@"Submit",nil]; 
     alert.tag = 1; 
     alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
     UITextField * alertTextField = [alert textFieldAtIndex:0]; 
     // assert(alertTextField); 
     alertTextField.delegate = self; 
     alertTextField.keyboardType = UIKeyboardTypeEmailAddress; 
     alertTextField.placeholder = @"Enter Email address"; 
     [alert show]; 
    } 

    contactEmailLbl.text = [emailArray objectAtIndex:0]; 

    //set all object to nil for memory management 
    dictAddress = nil; 
    addresses = nil; 
    addressesArray = nil; 

    phone = nil; 
    phoneArray = nil; 
    strPhone = nil; 

    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){ 
     [peoplePicker dismissViewControllerAnimated:YES completion:nil]; 
    } else { 
     [peoplePicker dismissModalViewControllerAnimated:YES]; 
    } 
    return NO; 
} 



// called to show detail of contact and user select contact 
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person 
           property:(ABPropertyID)property 
           identifier:(ABMultiValueIdentifier)identifier 
{ 
    return NO; 
} 
+1

Die Einbindung des <4.1-Codes verursacht in XCode 6.1 eine Warnung, da 4.1 ohnehin nicht mehr unterstützt wird. Dies ist jedoch die richtige Antwort, da es tatsächlich ein Codebeispiel darstellt – d2burke