Import contact from Address book to label - iphone

Is there any way to import a contact field(name, mobile phone, address) in a label?
(for iPhone app)

Yes, you can do it using the AddressBook framework.
Apple has instructions on how to retrieve contacts from your address book here.
Once you have retrieved a contact as an ABRecordRef, you can use the method ABRecordCopyValue(ABRecordRef record, ABPropertyID property) to get string values.
For example, to get a phone number, you could say (assuming curPerson is an already obtained ABRecordRef):
NSString *mobilePhone = (NSString *)ABRecordCopyValue(curPerson, kABPersonPhoneMobileLabel);

Related

Different count in Contacts app and ABAddressBookGetPersonCount

When I scroll My Contacts App to the bottom, where it shows the number of contacts, It says 2568 contacts.
But in my app when I try to fetch the number of contacts from AddressBook, by ABAddressBookGetPersonCount, I get 2582 contacts.
Has Anyone noticed this? Why is this difference there? Does the AddressBook framework give me wrong information??
EDIT
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSLog(#"num people to send: %ld", nPeople);
NSLog(#"num people in array: %ld", CFArrayGetCount(allPeople));
These two logs, give me the same number, however this number is different from that written at the bottom of the contacts app in iphone.
The contacts app pulls in its contacts from various groups. You can change the number of people in your list by going to the groups select page (top left in IOS 7) and selecting or deselecting your contacts.
If your list is pulling in all the contacts 'available' to you, the difference is due to contacts coming from a source not used in the Contacts App. You may have noticed that you can start to type in an email and it will auto fill the email even though you have no contact for that person. That information is stored inside the AddressBook database, but is not pulled into the Contact App. When you use the ABAddressBookCopyArrayOfAllPeople, it also pulls in these 'contacts'. You can verify this by putting all the contact's in a tableview and printing out the name, first email, first phone number, for each of the contacts. You will probably find a few contacts which only contain an email address (or possibly phone number).
An example of how to print off all the contacts can be found here.
Use this code
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName));
//CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
CFIndex nPeople = CFArrayGetCount(allPeople); // bugfix who synced contacts with facebook

how to add facebook's friends into iphone's address book

I'm new in iPhone development. I want to add facebook's friends name into iPhone's address book.
I can get get facebook's friend list from the facebook using facebook's sdk.framework.
Can someone help me to insert this friends name into iPhone address book?
You need to read Adress book programing from here
And for inserting contact: idea is -
You need make object of address book ABAddressBookRef addressBook = ABAddressBookCreate();
then create a record like this ABRecordRef person = ABPersonCreate();
then set some property like this
ABRecordSetValue(person, kABPersonFirstNameProperty, CFSTR(fbUserName), &anError);
fbUserName is the name of your friend getting by fbGraph api.
then you need to insert this record in address book ABAddressBookAddRecord();
then save the changes. ABAddressBookSave();
at last
CFRelease(addressBook);

AddressBook kABPersonEmailProperty returns nil for some contacts

I am using the following code to retrieve the email addresses of persons picked by the user
ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray *emailAddresses = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
Everything has worked fine since iOS6 and Facebook integration came. When I choose a contact that is not linked to a Facebook account (this means that I'm not friend on FB with this contact) everything works fine and I retrieve 2 email addresses (work and home).
But when I select a contact that is linked to me via Facebook (this contact has also a home and work email address plus the Facebook email address in the contact browser) the emailAddresses array is nil.
Does anyone had this problem before or has any hints on this?
Thanks in advance!
To filter out the Facebook contacts, you can check the kABSourceTypeProperty of the person. If it returns kABSourceTypeCardDAV, it may be from Facebook, but it most certainly not a local contact (which would return kABSourceTypeLocal).
Unfortunately, as of iOS 6, there is not a Facebook specific source type, so using kABSourceTypeCardDAV may filter out other sources that you may actually care about. In any case, here is some code;
ABRecordRef source = ABPersonCopySource(person);
NSNumber *sourceTypeRef = (__bridge NSNumber *)((CFNumberRef)ABRecordCopyValue(source, kABSourceTypeProperty)
if ([sourceTypeRef intValue] == kABSourceTypeCardDAV)
; // this is probably, maybe, could be a Facebook contact
if ([sourceTypeRef intValue] == kABSourceTypeLocal)
; // this is definitely a local contact
if ([sourceTypeRef intValue] == kABSourceTypeExchange)
; // this is from an exchange server
// etc...
I sortof found a way to check if a person is a facebook contact. In my case the problem was that facebook contacts are not editable, so I didnt want to show them in the "addressbook updater" function Im making.
(in the addressbook app from apple, you can edit facebook contacts, but what the app actually does is to create a new contact with the new information and links it to the facebook contact. I couldnt do this, because linking is not an public API in iOS.)
So to find those facebook contacts that I want to ignore, I just check if ABRecordSetValue returns true when I set the firstname to the current firstname, like this.
bool didset = ABRecordSetValue(person, kABPersonFirstNameProperty, ABRecordCopyValue(person, kABPersonFirstNameProperty), nil);
I found out that since iOS6 there are linked contacts (normal contact <-> facebook <-> twitter and so on...).
In order to get all emails of all linked contacts, the following post helped me:
iOS 6 address book empty kABPersonPhoneProperty
Hope it helps!

how to add birthday from my application to my phone address book

I have a task to develop the application related to address and access the birthdate from it. And if the user has to exists in the address then i can enter it manually from my application to address book.
I have to develop the application related to address book. From address book i have to get name,phone no,email, and birthday. so i can easily get it on my application. But for get i have set all thing from address book. But i also have to give facility to add the contact detail as above given field. and i can not find easily that how to add birthday in address book i have search a lot but i cant found it,
So,please help me and provide some sample code for it.
int recordId=123 //recordId in address book for record we want to update
//save birthday to address book
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID (addressBook,recordId);
CFErrorRef error = NULL;
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd.MM.yyyy"];
//don't set year in address book (yyyy=1604)
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:#"%#.1604",#"29.03"]];
[formatter release];
ABRecordSetValue(person, kABPersonBirthdayProperty,(CFDateRef)date, &error);
ABAddressBookSave (addressBook,&error);
CFRelease(addressBook);
Don't forget to import AddressBook/AddressBook.h file and add AddressBook.framework.
There is a demo app in Apple's code library called QuickContacts. You can download it and refer it. In that demo app there is a functionality where in you can create a new contact and it will save that contact in your iPhone's contacts.
Now, you can modify that controller and customize it as per your requirements. I didn't go down into the tiniest detail but I guess this would help you. You can download that demo app called QuickContacts from here.
You might have to work a little bit but I hope it helps!

iTunes Music Store Link Maker — how to search from within my app?

I'm writing a music reference app and for each album (pulled from last.fm) would like to link to the ITMS (if the album is in the store).
iTunes link maker web tool http://apple.com/itunes/linkmaker/ is great for getting links for a known album but I need to access it programatically from within my app.
This NSLog blogpost which is from 2003 but was referenced more recently in another question here seems to offer the only solution I've come across so far, suggesting to submit a query to:
phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?
Put "itms://" before it and the link will work in iTunes, put "http://" before it and the link will work in Camino (Safari sometimes spits back a malformed XML error).
The tags that are of importance are as follows:
songTerm - song title
artistTerm - artist name
albumTerm - album name
composerTerm - composer name
term - all fields
The suggestion is that would using http:// rather than itms:// the server will return an XML document of results instead of opening iTunes but either way I am sent directly to iTunes.
Is it possible to get back a list of results?
I am using LinkMaker to get iTunes details about song I am playing.
For that, I found that LinkMaker is able to return json data and also 1 result at a time.
I am using this url to perfom my query :
http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/itmsSearch?lang=1&output=json&country=%#&term=%#&media=%#&limit=1"
Here are parameters you need to give :
> country : store country term : could
> contains artist name, song name, album
> media : music
For exemple, if you want to have details for a song called "One" by "U2" here is the correct URL :
http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/itmsSearch?lang=1&output=json&country=US&term=U2%20one&media=music&limit=1
Then you will receive json data like this :
{
"resultCount":1,
"results": [
{"wrapperType":"track", "mediaType":"song", "artistName":"U2", "itemParentName":"Achtung Baby", "itemParentCensoredName":"Achtung Baby", "itemCensoredName":"One", "itemName":"One", "artistLinkUrl":"http://itunes.apple.com/us/artist/u2/id78500?uo=4", "artworkUrl60":"http://a1.phobos.apple.com/us/r1000/009/Features/32/9a/60/dj.mfynlttx.60x60-50.jpg", "artworkUrl100":"http://a1.phobos.apple.com/us/r1000/009/Features/32/9a/60/dj.mfynlttx.100x100-75.jpg", "country":"USA", "currency":"USD", "discCount":1, "discNumber":1, "itemExplicitness":"notExplicit", "itemLinkUrl":"http://itunes.apple.com/us/album/one/id368713?i=368617&uo=4", "itemPrice":"1.29000", "itemParentExplicitness":"notExplicit", "itemParentLinkUrl":"http://itunes.apple.com/us/album/one/id368713?i=368617&uo=4", "itemParentPrice":"9.99000", "previewUrl":"http://a1.phobos.apple.com/us/r1000/019/Music/b6/8c/c5/mzm.epegonxg.aac.p.m4a", "primaryGenreName":"Rock", "trackCount":12, "trackNumber":3, "trackTime":276042}]
}
You need then to decode these JSON data.
NSDictionary *jsonResultsParsed = [jsonResults JSONValue];
And finally get what you want :
NSDictionary *songDetailsDict = [[jsonResultsParsed objectForKey:#"results"] objectAtIndex:0];
If you want to determine user's country you will need to determine its country using its locale, here is the code I am using :
- (NSString *)getUserCountry
{
NSLocale *locale = [NSLocale currentLocale];
return [locale objectForKey: NSLocaleCountryCode];
}
Hope this helps.
Thierry
Edit: Finally a doc is available:
http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
This document for iTunes Store Web Service Search API (pdf), although old and incomplete, seems to be the way to accomplish this.
It's a painful experience setting this up though, as has been every other part of the affiliate programme.