Pretty interesting question regarding address book in iPhone - iphone

HI all i have seen the example of apple name as 'QUICK CONTACTS' and here is an error in the
person view controller. here it always show same data with two names
// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// Search for the person named "Appleseed" in the address book
NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));
// Display "Appleseed" information if found in the address book
if ((people != nil) && [people count])
{
ABRecordRef person = (ABRecordRef)[*people objectAtIndex:0*];
ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];
picker.personViewDelegate = self;
picker.displayedPerson = person;
// Allow users to edit the person’s information
picker.allowsEditing = YES;
[self.navigationController pushViewController:picker animated:YES];
}
here it always show same information for two person with same name how can we remove this error.

quite confused with what you wanna ask.. if you are getting same data that means you the same data feed in two times ..are you working on a simulator ?

Related

Retrieve selected group from native contact application

I am working on one application in which I need to show contacts from the contact application.
I have successfully implemented the functionality but now I need to only show the contacts from the groups which are selected in native contact application similar Magic Jack(http://itunes.apple.com/us/app/free-calls-with-magicjack/id463926997?mt=8) iPhone app.
I have tried to search on google, forums, apple's documentation but didn't able to find the solution. Can anyone help me?
I am using below code to fetch all contact information.
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
NSArray *persons = (NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName));
Here is code you can use,it fetch the first and last name.
-(IBAction)handlingClick:(id)sender
{
ABPeoplePickerNavigationController *peopleController = [[ABPeoplePickerNavigationController alloc] init];
peopleController.peoplePickerDelegate = self;
[self presentModalViewController:peopleController animated:YES];
[peopleController release];
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[peoplePicker dismissModalViewControllerAnimated:YES];
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
self.firstName.text = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
self.lastName.text = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Is ABGroupCopyArrayOfAllMembers not suitable for what you want?

Setting person properties

Working on someone else's code. I am really confused about whats exactly happening in this code.
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef persons = ABAddressBookGetPersonWithRecordID(addressBook, x);
CFErrorRef *error=NULL;
ABAddressBookRemoveRecord(addressBook, persons, error);
ABAddressBookSave(addressBook, error);
ABRecordRef persons = ABPersonCreate();
ABRecordSetValue(persons, kABPersonFirstNameProperty, firstName , nil);
What should be done is that, a ABRecordRef person should be created. His properties should be set and his record/details should be displayed. He should never be saved in the address book. Is this the way to do it. Need help.
Edit: Apart from setting properties to person, the following code is added to push the view controller to view the person.
MyContactDetailViewcontroller *personContactDetail = [[MyContactDetailViewcontroller alloc] init];
personContactDetail.displayedPerson = persons;
personContactDetail.passedSelectedContactData = selectedContactsOnlyData;
[self.navigationController pushViewController:personContactDetail animated:YES];
[personContactDetail release];
MyContactDetailViewcontroller subclass ABPersonViewController. Or is it more apt to use ABUnknownPersonViewController.
Do you find any cases where the contacts could be duplicated in address book
This is exactly what is happening here.. I will explain..
ABAddressBookCreate creates a new address book from data from the addressbook database, so any changes you make to ABAddressBookRef will only be saved to actual addressbook database once you call ABAddressBookSave(). so what it is doing is getting the reference for the person with recordid- x. Than you are creating a new person entry using
ABRecordRef persons = ABPersonCreate();
and than you are setting its value, but this is available to this particular object and not stored in the database unless you call... ABAddressBookSave()
hoping this helps you... :)

Custom addressbook for ABPeoplePickerNavigationController on iPhone

I am trying to create a custom addressbook on the iPhone by adding new records to the device address book (basically, merging a private contact list with the device AB). I then want to let the user select a contact, which I thought I could do with the .addressbook property of ABPeoplePickerNavigationController:
ABAddressBookRef contacts = ABAddressBookCreate();
for (PrivateUserType *user in rosterItems)
{
CFErrorRef err = NULL;
ABRecordRef ref = ABPersonCreate();
ABRecordSetValue(ref, kABPersonLastNameProperty, (CFStringRef)user.lastName, &err);
if (err != NULL)
{
NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
NSLog(#"Can not set name: %#", errorStr);
}
...
ABAddressBookAddRecord(contacts, ref, &err);
if (err != NULL)
{
NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
NSLog(#"Error adding XMPP roster user to addressbook: %#", errorStr);
}
}
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.addressBook = contacts;
[viewController presentModalViewController:picker animated:YES];
[picker release];
The entries are successfully added to the addressbook (which I can verify if I do an ABAddressBookSave() after the loop to add everything in). However, the entries are not shown by picker (unless I save contacts, which of course saves all changes into the device addressbook).
Is there a way to do this? This is on iOS 4.3.1.
Thanks!
As Toro said, you need save any changes to database first. ABPeoplePickerNavigationController not intended to work with uncommit data or with custom data.
You can see rather fresh topic on Apple Dev Forums
https://devforums.apple.com/message/370070#370070
where similar question was asked. And guy from Apple says that "I can't see any way to do this." But he suggest filling a bug for this to implement this feature in future release of iOS.
All you can do now is code your own UI for this AddressBook from the ground :(
updated: he-he. I found your question on App Dev Forum too )))
You need to save any changes to database by ABAddressBookSave() method.
The apple doc link.

Iphone sms app issue

iam making an application where i have to send a same body sms to my contact list . I m using
Class messageClass = (NSClassFromString(#"MFMessageComposeViewController"));
if (messageClass != nil)
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
if ([appdelegate.NumberArray count] > 0) {
NSString *aa = [appdelegate.NumberArray objectAtIndex:0];
NSMutableArray *myArray = [aa componentsSeparatedByString:#","];
NSString *n = [myArray objectAtIndex:0];
NSString *m=[myArray objectAtIndex:1];
NSString *new = [appdelegate.globalmsg stringByReplacingOccurrencesOfString: #"forgot" withString:n];
controller.body = new;
controller.recipients= [NSArray arrayWithObject:m];
controller.messageComposeDelegate = self;
//if([appdelegate.NumberArray count] ==2 ){
[self presentModalViewController:controller animated:YES];
im taking out my contact one by one from my mutable array . the problem is that my program sending an sms one at a time, not to the complete list. and if i wanted to send sms to next person i have to click send button again. is there any way i can send sms to everyone without clicking that send button again again?
u can check this app .... http://itunes.apple.com/us/app/automatic-custom-sms/id409247779 in this app last page comes automatically and send everyone from your contact list. ??
Ok i fix it. i use a local variable an initialize it in view did load with value 0 and then in view did appear update it to 1 and make making present model view appear till my array finished.

How to search iPhone address book for specific user name based on variable and not a string

Apple provides the following sample code in their QuickContacts project for how to search the address book for a particular user.
-(void)showPersonViewController
{
// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// Search for the person named "Appleseed" in the address book
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));
// Display "Appleseed" information if found in the address book
if ((people != nil) && (CFArrayGetCount(people) > 0))
{
ABRecordRef person = CFArrayGetValueAtIndex(people, 0);
ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];
picker.personViewDelegate = self;
picker.displayedPerson = person;
// Allow users to edit the person’s information
picker.allowsEditing = YES;
[self.navigationController pushViewController:picker animated:YES];
}
else
{
// Show an alert if "Appleseed" is not in Contacts
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Could not find Appleseed in the Contacts application"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
CFRelease(addressBook);
CFRelease(people);
}
The line that I'm having trouble with is:
// Search for the person named "Appleseed" in the address book
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));
This will search the address book for the person named "Appleseed", but I want to search the address book based on a user that is stored in a variable. For example, I'm trying:
Customer *customer = [customerArray objectAtIndex:indexPath.row];
cell.textLabel.text = customer.name;
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR(customer.name));
The "customer.name" isn't resolving to the value that is stored. I used NSLog to output the value of customer.name and it holds the expected value.
How can I resolve this variable to a string so it will search the address book properly?
Thanks!
Is customer.name an NSString?
CFSTR only accepts literal C strings.
To pass an NSString, cast it to CFStringRef:
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook,
(CFStringRef)customer.name);
only if someone has the same problem - be avare that contacts in iOS simulator cannout be found using ABAddressBookCopyPeopleWithName until you rename them, they are somehow broken. if you want to test, sync contacts from facebook, or something like that