0

Trying to allow user to save contact details from my app to their address book/contacts.

under IBAction:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate(); 


CFErrorRef  anError = NULL;

ABRecordSetValue(person,kABPersonFirstNameProperty,@"Name goes here",&anError);
ABRecordSetValue(person, kABPersonOrganizationProperty, @"Company name here", &anError);
ABRecordSetValue(person, kABPersonJobTitleProperty, @"Job Title Here", &anError);

ABMutableMultiValueRef multiAddress =   ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

[addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];

ABMultiValueAddValueAndLabel(multiAddress, @"Adress", (CFStringRef)@"Address", NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&anError);
CFRelease(multiAddress);

ABMutableMultiValueRef websiteMultiValue = ABMultiValueCreateMutable(kABPersonURLProperty);
ABMultiValueAddValueAndLabel(websiteMultiValue, @"www.7app.co.uk", (CFStringRef)@"Website", NULL);
ABRecordSetValue(person, kABPersonURLProperty, websiteMultiValue, &anError);


ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
ABMultiValueAddValueAndLabel(emailMultiValue, @"something@hotmail.com", (CFStringRef)@"Email", NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

ABAddressBookAddRecord(addressBook, person, &anError);





}

Ive imported

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI>.h

Im getting no response at all when I press the button. Nothing at all happens. Just to mention this is my first attempt at using the address book framework if theres a glaring error.

I also wanted to save a contact pic with the following

 NSData *dataRef = UIImagePNGRepresentation(my image here);
ABPersonSetImageData(person, (__bridge_retained CFDataRef)dataRef, nil);

However I get an exception thrown, ive decared an image in the ususal manner, not sure if that is appropriate for this method? Error msg im getting:

 2012-07-17 19:01:50.132 543[966:707] -[UIImageView CGImage]: unrecognized selector sent     to instance 0x15b9b0
 2012-07-17 19:01:50.133 543[966:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView CGImage]: unrecognized selector sent to instance 0x15b9b0'

Normally when an exception is thrown its because of connection problem, I have double checked and the UI image has no duplicate connections etc.

Any advice as always appreciated

JSA986
  • 5,870
  • 9
  • 45
  • 91

2 Answers2

1

ABAddressBookSave()

http://developer.apple.com/library/ios/#documentation/AddressBook/Reference/ABAddressBookRef_iPhoneOS/Reference/reference.html

Re your image problem, it looks like you are passing a UIImageView instance to UIImagePNGRepresentation() rather than a UIImage instance

Peter Cetinski
  • 2,328
  • 14
  • 8
  • Thanks for reply, yes I have those links to Apple documentation, unfortunately it isn't all that easy to understand and not particularly user friendly for those with not much programming experience. When you say an UIImage Instance, is that the name of the image file in the supporting files rather than the declared UI image view?, If so tried that I get and get undeclared identifier error. – JSA986 Jul 17 '12 at 18:22
  • UIImageView has a property named "image" which is a UIImage. That is the representation of the image you want convert to PNG. – Peter Cetinski Jul 17 '12 at 18:24
  • Ok thats sorted the image problem out, thanks, have a plus 1! Just have to sort out the other bit. Thanks for your help on the image part of it. – JSA986 Jul 17 '12 at 18:52
0

Ok sorted the no action problem, had to implement the code to present the view!

JSA986
  • 5,870
  • 9
  • 45
  • 91