FunForDay Application

Posted: August 7, 2013 in FunForDay

This application is for all people. FunForDay will daily send you one funny video or funny image. You can share link with others, it will increase your points so if your points will be highest than you will get prize. Even you can upload new videos or picture.

Add the contacts to Address Book

Posted: December 2, 2010 in Address Book

Programmatically you can add the contacts in Address Book. but if you have more contacts than it is going to take long time. Reason is each and every time you need to create the person object to add the contact detail. So think suppose you have a 500 contacts and you need to add them in Address Book. how much time is needed ? almost it will take more than 10 min.

But still i wanna show you. Below is the code to add the contacts in Address Book.

ABAddressBookRef addressBook = ABAddressBookCreate();

for(ZKSObject *obj in contactsArr)
{
ZKSObject *accountObj = [obj fieldValue:@”Account”];
NSString *accountName = [accountObj fieldValue:@”Name”];

ABRecordRef person = ABPersonCreate();
CFErrorRef  error = NULL;

// kABStringPropertyType

if(accountName != nil)
ABRecordSetValue(person, kABPersonOrganizationProperty, accountName, &error);
if([obj fieldValue:@”Birthdate”] != nil)
{
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@”yyyy-MM-dd”];
NSDate *date = [formatter dateFromString:[obj fieldValue:@”Birthdate”]];
[formatter release];

BOOL success = ABRecordSetValue(person, kABPersonBirthdayProperty,(CFDateRef)date, &error);
if (!success) NSLog(@”Error: %@”, [(NSError *)error localizedDescription]);
}

if([obj fieldValue:@”Department”] != nil)
ABRecordSetValue(person, kABPersonDepartmentProperty, [obj fieldValue:@”Department”], &error);
if([obj fieldValue:@”Description”] != nil)
ABRecordSetValue(person, kABPersonNoteProperty, [obj fieldValue:@”Description”], &error);
if([obj fieldValue:@”Title”] != nil)
ABRecordSetValue(person, kABPersonJobTitleProperty, [obj fieldValue:@”Title”], &error);
if([obj fieldValue:@”FirstName”] != nil)
ABRecordSetValue(person, kABPersonFirstNameProperty, [obj fieldValue:@”FirstName”], &error);
if([obj fieldValue:@”LastName”] != nil)
ABRecordSetValue(person, kABPersonLastNameProperty, [obj fieldValue:@”LastName”], &error);

// kABMultiStringPropertyType

ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
if([obj fieldValue:@”Phone”] != nil)
{
ABMultiValueAddValueAndLabel(multiPhone,[obj fieldValue:@”Phone”], kABWorkLabel, NULL);
}
if([obj fieldValue:@”OtherPhone”] != nil)
{
ABMultiValueAddValueAndLabel(multiPhone,[obj fieldValue:@”OtherPhone”], kABOtherLabel, NULL);
}
if([obj fieldValue:@”HomePhone”] != nil)
{
ABMultiValueAddValueAndLabel(multiPhone,[obj fieldValue:@”HomePhone”], kABHomeLabel, NULL);
}
if([obj fieldValue:@”MobilePhone”] != nil)
{
ABMultiValueAddValueAndLabel(multiPhone,[obj fieldValue:@”MobilePhone”], kABPersonPhoneMobileLabel, NULL);
}
if([obj fieldValue:@”Fax”] != nil)
{
ABMultiValueAddValueAndLabel(multiPhone,[obj fieldValue:@”Fax”], kABPersonPhoneWorkFAXLabel, NULL);
}
ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
CFRelease(multiPhone);

if([obj fieldValue:@”Email”] != nil)
{
ABMutableMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(email, [obj fieldValue:@”Email”], CFSTR(“email”), NULL);
ABRecordSetValue(person, kABPersonEmailProperty, email, &error);
CFRelease(email);
}

// kABMultiDictionaryPropertyType

ABMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];

if([obj fieldValue:@”MailingStreet”] != nil)
[addressDict setObject:[obj fieldValue:@”MailingStreet”] forKey:(NSString *)kABPersonAddressStreetKey];
if([obj fieldValue:@”MailingCity”] != nil)
[addressDict setObject:[obj fieldValue:@”MailingCity”] forKey:(NSString *)kABPersonAddressCityKey];
if([obj fieldValue:@”MailingState”] != nil)
[addressDict setObject:[obj fieldValue:@”MailingState”] forKey:(NSString *)kABPersonAddressStateKey];
if([obj fieldValue:@”MailingCountry”] != nil)
[addressDict setObject:[obj fieldValue:@”MailingCountry”] forKey:(NSString *)kABPersonAddressCountryKey];
if([obj fieldValue:@”MailingPostalCode”] != nil)
[addressDict setObject:[obj fieldValue:@”MailingPostalCode”] forKey:(NSString *)kABPersonAddressZIPKey];

ABMultiValueAddValueAndLabel(address, addressDict, kABWorkLabel, NULL);

NSMutableDictionary *addresDict = [[NSMutableDictionary alloc] init];

if([obj fieldValue:@”OtherStreet”] != nil)
[addresDict setObject:[obj fieldValue:@”OtherStreet”] forKey:(NSString *)kABPersonAddressStreetKey];
if([obj fieldValue:@”OtherCity”] != nil)
[addresDict setObject:[obj fieldValue:@”OtherCity”] forKey:(NSString *)kABPersonAddressCityKey];
if([obj fieldValue:@”OtherState”] != nil)
[addresDict setObject:[obj fieldValue:@”OtherState”] forKey:(NSString *)kABPersonAddressStateKey];
if([obj fieldValue:@”OtherCountry”] != nil)
[addresDict setObject:[obj fieldValue:@”OtherCountry”] forKey:(NSString *)kABPersonAddressCountryKey];
if([obj fieldValue:@”OtherPostalCode”] != nil)
[addresDict setObject:[obj fieldValue:@”OtherPostalCode”] forKey:(NSString *)kABPersonAddressZIPKey];

ABMultiValueAddValueAndLabel(address, addresDict, kABOtherLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, address, &error);
CFRelease(address);

ABAddressBookAddRecord(addressBook, person, &error);
ABAddressBookSave(addressBook, &error);

CFRelease(person);
}

CFRelease(addressBook);