I am trying to get the email count of a contact from addressbook. This is what I tried.
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray* emails = (NSArray*)ABMultiValueCopyArrayOfAllValues(email);
if([emails count] < 1){
//things to do
}
But I am not getting the count right(count is always nil). What am I doing wrong here? Whats the right way to do this?
Have you checked if email is null?
Anyway, this should work:
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int size = ABMultiValueGetCount(email);
The code I have given was right. The problem was someone who gave their email address in the url label which was misleading me all this time, and I could find it only when I tried to edit the addressbook. Apologies for the stupid question. Thank you.
Related
In my app I want to get the names and emails of the phone contacts who has emails then list them in a tableview.
Is it possible to create user objects with the name and email fields which are filled by the address book of the phone then put those object in a tableview?
Any clue will be appreciated.
The approach you need to follow is :
First you need to get the reference of the AddressBook and then get all the references of contacts in an array. And then you need to enumerate the array and check whether their email property is nil, if not nil, add that contact to another array and then use that array as a datasource for the tableView :)
If you want, I can provide you with the code for checking the email entry for contact in addressBook and then filter those contacts.
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 =(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:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:#"email"];
}
With the help of you will get to know, whether the contact has an email or not, if it has, then you can add that contact reference to other array :) Hope it helps :)
You can follow this link - http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html
ABAddressBook class lets you to access the address book data. And you can create your own custom object to these.
kABPersonEmailProperty lets you get the email property of an ABPerson
In my app I import a contact details using addressbookui.In case of email or mobile number empty my app getting crashed.
For email try this:
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0)
{
NSString *personEmail = (NSString *)ABMultiValueCopyValueAtIndex(eMail, 0);
}
Using the ABPersonCreateVCardRepresentationWithPeople method for export, and the ABPersonCreatePeopleInSourceWithVCardRepresentation for import, I have successfully transfered contact data between devices. However, the data in the contact's "notes" field isn't transfered.
Here's my export function:
+(NSData*)exportContactsToVcard:(NSArray*)contacts
{
NSMutableArray *people = [NSMutableArray arrayWithCapacity:contacts.count];
ABAddressBookRef ab = ABAddressBookCreate();
for (Contact *contact in contacts)
{
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,contact.contactId);
[people addObject:(__bridge id)person];
}
NSData *vCard = (__bridge NSData*)ABPersonCreateVCardRepresentationWithPeople((__bridge CFArrayRef) people);
return vCard;
}
and part of my import function:
+(NSArray*)importContactsFromVcardData:(NSData*)vcardData
{
NSMutableArray *addedContactIds = [NSMutableArray array];
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
NSArray *createdPeople = (__bridge_transfer NSArray*)ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource,(__bridge CFDataRef)vcardData);
CFErrorRef error = NULL;
for (id person in createdPeople)
{
error = NULL;
ABRecordRef personRecord = (__bridge ABRecordRef)person;
NSString *notes = (__bridge NSString *)ABRecordCopyValue(personRecord, kABPersonNoteProperty);
In the last line, notes is always nil, even if the contact had notes before it was exported. All the other standard contact fields seem to be in place.
For example, if I replace the last line with:
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(personRecord, kABPersonFirstNameProperty);
the firstName string will hold the contact's first name.
Any idea how I can work around this, and get the contact notes?
Thanks.
For testing purposes, you can export a vCard from Address Book. Then drag it to TextEdit to look at the various fields.
At the bottom you'll find something like this:
NOTE:This is a note!
Also, see this link for info on the vCard format:
http://en.wikipedia.org/wiki/VCard
ABPersonCreateVCardRepresentationWithPeople will only transfer the important contact details of a vCard...thats my identifying in much tests with the vCard and the iOS import-export function. It has many mistakes, e.g. if you try to import it with social networks like facebook, it won't do it with (just look in my other thread THREADLINK )
But if you try to add some vCard Information over ABNewPersonViewController, it will work perfectly.
I follow this instruction to add vcard data to addressbook, in my project. iOS 5.
Adding vCard data directly to the system Address Book
The code run ok, and add vcard data to adressbook in my ipad2 and iphone4s, however, xcode show this error:
thread 1 program received signal: "exc_bad_acess"
Why this error appears?
EDIT: code add.
NSString *vCardString =
#"BEGIN:VCARD\n"
#"N:Paulo;Joao\n"
#"TEL:+44 (0)9999 8888\n"
#"ADR;HOME:;;Flat 2, 43 Howitt Road, Belsize Park;London;;NW34LU;UK\n"
#"ORG:NetTek Ltd;\n"
#"TITLE:Consultant\n"
#"EMAIL:none#none.com\n"
#"URL:www.none.com\n"
#"BDAY:19001010\n"
#"END:VCARD";
CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++)
{
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(book, person, NULL);
CFRelease(person);
}
CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(book, NULL);
CFRelease(book);
I found the problem.
The problem is with the last line, probably because of ARC.
CFRelease(book);
I commented the line, and the program run normally.
I'm glad you were able to find the problem, however, I just thought I'd let you know that it's not an ARC thing but a memory leak thing. You're over-releasing data the the OS wants to release itself. Here's an explanation.
Additionally, please mark the question as answered so you can help other people find the solution. Thank you!
How can I get the Twitter email id list in the iPhone sdk?
I'm using MGTwitterEngine and when I try the following method:
NSString *str2 = [_engine getUserInformationForEmail:#"yw8181#gmail.com"];
NSLog(#"User Email Id:--->%#",str2);;
so its return 0E156D0F-3E56-4935-9BE9-2A34786545ED so how can i display in tableView
I'm not getting email id list, please help me solve this issue.
What happens when you log it?
I'm guessing that it doesn't return an NSString, but another datatype like an NSArray?
Try this:
NSArray *myArray = [_engine getUserInformationForEmail:#"yw8181#gmail.com"];
for (NSString *item in myArray) {
NSLog(#"%#",item);
}