开发者

How to add a string to address book in an iOS application?

I dont know anything about Objective-C and Xcode, almost nothing, I tried building only with very basic apps.

Now I开发者_开发问答 have here an app porject source, a friend of mine built partially, and I need to finish it.

The only thing I need is to save the string (a telephone number in my case) to the iphone address book.

This is the var

resultText.text 

that has the string coming from a function. So I just need to find the right code to end the action btw, I already added the Adressbook /and UI frameworks.

Thanks!


Here, you will need to link the the ABAddressBook framework first though. To do that, you will need to (in XCode) goto Project > Edit active target "TargetNameHere.app". Press the add button on the lower left hand corner, and find AddressBook.framework (is usually the first there). Click add.

import <AddressBook/AddressBook.h>

// CODE BETWEEN START OF FILE AND ADDING ADRESS BOOK ENTRY.

ABAddressBookRef ab = ABGetSharedAddressBook();
// To add a new ab entry with telephone number
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"Bob", nil);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Jones", nil);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, resultText.text, nil);

ABAddressBookAddRecord(ab, newPerson, nil);
ABAddressBookSave(ab);

For the sake of space, I didn't check for NULL, or pass in error pointers and check for errors, but you should do this.

This code should go in a .m file of the class that wants to add an Address Book entry; most likely in the function that currently has access to resultText.text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜