Add AddressBook.Framework to the Project
#import <AddressBook/AddressBook.h>
-(NSUInteger) copyContactToAB{
NSUInteger addressbookId = 0;
UIImage *imgContact = nil;//[UIImage imageNamed:@"test.png"];
NSString *companyName = @"Test";
NSString *firstName= @"Hanoch";
NSString *lastName= @"J";
NSString *workPhone = nil;
NSString *workFax = nil;
NSString *mobile = nil;
NSString *homePhone = nil;
NSString *workEmail = nil;
//work address
NSString *address1_street = nil;
NSString *address1_city = nil;
NSString *address1_state = nil;
NSString *address1_postalcode = nil;
NSString *address1_country = nil;
//home address
NSString *address2_street = nil;
NSString *address2_city = nil;
NSString *address2_state = nil;
NSString *address2_postalcode = nil;
NSString *address2_country = nil;
ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &anError);
ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &anError);
if(companyName){
ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &anError);
}
if (anError != NULL) {
MELog(@"error while creating..%@", anError);
}
if(imgContact){
NSData *data = UIImagePNGRepresentation(imgContact);
if(ABPersonSetImageData(aRecord, (CFDataRef)data, &anError)){
}
}
//(@"adding email");
if(workEmail){
ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emails, workEmail, kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, emails, &anError);
CFRelease(emails);
}
//(@"adding phonee");
ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
if(workPhone) ABMultiValueAddValueAndLabel(multi, workPhone, kABWorkLabel, NULL) ;
if(workFax) ABMultiValueAddValueAndLabel(multi, workFax, kABPersonPhoneWorkFAXLabel,NULL) ;//&&
if(mobile) ABMultiValueAddValueAndLabel(multi, mobile, kABPersonPhoneMobileLabel,NULL);
if(homePhone) ABMultiValueAddValueAndLabel(multi, homePhone, kABHomeLabel,NULL);
if(workPhone || workFax || mobile || homePhone){
ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
if (anError != NULL) { MELog(@"error while creating ABMutableMultiValueRef..%@", anError); }
}
CFRelease(multi);
//add work adress
ABMutableMultiValueRef multiAddress1 = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init];
if(address1_street) [addressDictionary1 setObject:address1_street forKey:(NSString *) kABPersonAddressStreetKey];
if(address1_city) [addressDictionary1 setObject:address1_city forKey:(NSString *)kABPersonAddressCityKey];
if(address1_state) [addressDictionary1 setObject:address1_state forKey:(NSString *)kABPersonAddressStateKey];
if(address1_postalcode) [addressDictionary1 setObject:address1_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
if(address1_country) [addressDictionary1 setObject:address1_country forKey:(NSString *)kABPersonAddressCountryKey];
//[addressDictionary setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey];
bool didAdd = NO;
if([addressDictionary1 count] > 0){
didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary1, kABWorkLabel, NULL);
}
[addressDictionary1 release];
//add home address
NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
if(address2_street) [addressDictionary2 setObject:address2_street forKey:(NSString *) kABPersonAddressStreetKey];
if(address2_city) [addressDictionary2 setObject:address2_city forKey:(NSString *)kABPersonAddressCityKey];
if(address2_state) [addressDictionary2 setObject:address2_state forKey:(NSString *)kABPersonAddressStateKey];
if(address2_postalcode) [addressDictionary2 setObject:address2_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
if(address2_country) [addressDictionary2 setObject:address2_country forKey:(NSString *)kABPersonAddressCountryKey];
//[addressDictionary setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey];
if([addressDictionary2 count] > 0 ){
didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary2, kABHomeLabel, NULL);
}
if(didAdd){
//(@"home address added");
anError = NULL;
ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAddress1, &anError);
if (anError != NULL) { MELog(@"error while creating home address..%@", anError); }
}
CFRelease(multiAddress1);
[addressDictionary2 release];
addressDictionary2 = nil;
////////////
ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
/*BOOL isAdded =*/ ABAddressBookAddRecord (addressBook, aRecord, &error);
if (error != NULL) {
MELog(@"ABAddressBookAddRecord %@", error);
}
error = NULL;
if(ABAddressBookSave ( addressBook, &error)){
addressbookId = ABRecordGetRecordID (aRecord);
}
if (error != NULL) {
MELog(@"ABAddressBookSave %@", error);
}
CFRelease(aRecord);
CFRelease(addressBook);
return addressbookId;
}
Monday, June 25, 2012
iOS - Add Contact to AddressBook - Sample code
Labels:
iOS sdk,
iPhone/iPad
Subscribe to:
Post Comments (Atom)
good tutorial
ReplyDeleteözel ambulans
ReplyDeleteminecraft premium
nft nasıl alınır
yurtdışı kargo
lisans satın al
uc satın al
en son çıkan perde modelleri
en son çıkan perde modelleri
Good content. You write beautiful things.
ReplyDeletesportsbet
korsan taksi
vbet
hacklink
taksi
vbet
mrbahis
sportsbet
hacklink
başakşehir
ReplyDeletebayrampaşa
beşiktaş
beykoz
beylikdüzü
K2MU
mecidiyeköy
ReplyDeletesakarya
istanbul
kayseri
ordu
3L8Q
van
ReplyDeleteerzincan
sivas
ağrı
manisa
UE7V4
369CA
ReplyDeleteKocaeli Evden Eve Nakliyat
Kırıkkale Parça Eşya Taşıma
Kütahya Parça Eşya Taşıma
Balıkesir Evden Eve Nakliyat
Düzce Evden Eve Nakliyat
AB376
ReplyDeleteSivas Şehirler Arası Nakliyat
Silivri Cam Balkon
Antep Şehirler Arası Nakliyat
Yozgat Evden Eve Nakliyat
Muğla Lojistik
Bilecik Lojistik
Uşak Şehir İçi Nakliyat
Siirt Şehir İçi Nakliyat
Ünye Çelik Kapı
A4924
ReplyDeleteKırklareli Şehirler Arası Nakliyat
Kırklareli Şehir İçi Nakliyat
Btcturk Güvenilir mi
Tekirdağ Fayans Ustası
Nevşehir Şehirler Arası Nakliyat
Pursaklar Fayans Ustası
Gümüşhane Lojistik
Antalya Lojistik
Mamak Parke Ustası
8610A
ReplyDeleteKırıkkale Şehir İçi Nakliyat
Çerkezköy Bulaşık Makinesi Tamircisi
Çorlu Lojistik
Pursaklar Fayans Ustası
Çorum Parça Eşya Taşıma
Shinja Coin Hangi Borsada
Van Şehirler Arası Nakliyat
Ünye Çekici
Sivas Evden Eve Nakliyat
406E2
ReplyDeleteMalatya Parça Eşya Taşıma
Muğla Parça Eşya Taşıma
Pursaklar Parke Ustası
Ardahan Lojistik
Adıyaman Şehirler Arası Nakliyat
Aksaray Şehirler Arası Nakliyat
Hatay Şehir İçi Nakliyat
Malatya Şehirler Arası Nakliyat
Çerkezköy Marangoz
8FA26
ReplyDeletebinance indirim kodu
B0A7C
ReplyDeleteCoin Kazanma Siteleri
Kripto Para Nasıl Alınır
Bitcoin Nasıl Para Kazanılır
Mexc Borsası Güvenilir mi
Bitcoin Nasıl Oynanır
Binance'de Kaldıraç Var mı
resimlimag.net
Binance Ne Kadar Komisyon Alıyor
Bitcoin Kazanma
AD03D
ReplyDeleteTrovo Takipçi Satın Al
Ort Coin Hangi Borsada
Telegram Abone Hilesi
Onlyfans Takipçi Hilesi
Tumblr Beğeni Satın Al
Twitch Takipçi Satın Al
Binance Kaldıraçlı İşlem Nasıl Yapılır
Coin Üretme
Bitcoin Para Kazanma
A5450
ReplyDeleteledger wallet
arbitrum
sushi
bitbox
shiba
phantom
quickswap
dappradar
metamask