how to get open contacts by calling method instead of button click - iphone

As per my requirement,
i need to get email id's from contacts.
And i need to write a code for this in a separate class in a method.To get call this integrate classes into my project simply call that method.
This is what i need.
for this my code in ownServices is like this.
-(NSString *)getSelectedNumberFromContatcs {
ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
peoplePickerController.peoplePickerDelegate = self;
[self presentModalViewController:peoplePickerController animated:NO];
[peoplePickerController release];
return aNSString;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
// NSString *name = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty) {
ABMultiValueRef emails = ABRecordCopyValue(person, property);
CFStringRef phonenumberselected = ABMultiValueCopyValueAtIndex(emails, identifier);
// CFStringRef emailLabelSelected = ABMultiValueCopyLabelAtIndex(emails, identifier);
// CFStringRef emailLabelSelectedLocalized = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, identifier));
aNSString = (NSString *)phonenumberselected;
// Return to the main view controller.
[ self dismissModalViewControllerAnimated:YES ];
return NO;
}
return YES ;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[ self dismissModalViewControllerAnimated:YES ];
}
i am calling this in myclassviewcontroller like this.
- (void)viewDidLoad {
[super viewDidLoad];
ownServices *obj = [[ownServices alloc]init];
[obj getSelectedNumberFromContatcs];
}
But contatcs viewcontraoller is not opened.
But i try same code in view controller in a button action like this
-(IBAction)openContacts {
ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
peoplePickerController.peoplePickerDelegate = self;
[self presentModalViewController:peoplePickerController animated:NO];
[peoplePickerController release];
}
Then contacts viewconrtroller opened.
i did n't why view controller is not opened by calling it in a method.
is it possible to do like this.
can any one please help me.
Thank u in advance.

Because the class you are using is inherited from NSObject not UIViewController so the [self presentModalViewController:peoplePickerController animated:NO]; will not work. Also
you have written this method in a wrong way
-(NSString *)getSelectedNumberFromContatcs {
ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
peoplePickerController.peoplePickerDelegate = self;
[self presentModalViewController:peoplePickerController animated:NO];
[peoplePickerController release];
return aNSString;
}
Let me know what actually you want to do?

Instead of peoplePickerController.peoplePickerDelegate = self is it possible to use the reference of your main View Controller?

Related

ABPeoplePickerNavigationControllerDelegate: shouldContinueAfterSelectingPerson giving bad ABMultiValue id with email addresses

This works fine when I select contacts that have multiple phone numbers, and pick one of their phone numbers, recipientAddress is set to the selected phone number. But when I select email addresses from contacts having multiple email addresses, the ABMultiValueIdentifier is zero, and it translates into an index of zero, which is always the last email in the contact, regardless of which I selected.
I must be doing something embarrassingly wrong and easy to find, so please make yourself look great by exposing my foolishness.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
#try {
[eta addRecipient: person : property: identifier];
}
#catch (NSException *exception) {
errExcLog(exception);
}
return NO;
}
- (void) addRecipient : (ABRecordRef) person : (ABPropertyID) property : (ABMultiValueIdentifier)identifier {
ABMultiValueRef mvPropertyRef = ABRecordCopyValue(person, property);
int propertyIndex = ABMultiValueGetIndexForIdentifier( mvPropertyRef, identifier);
NSString *recipientAddress = (__bridge NSString *)(ABMultiValueCopyValueAtIndex( mvPropertyRef, propertyIndex));
}
These two methods might help you to get the selected person's email id
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
ABPersonViewController *controller = [[ABPersonViewController alloc] init];
controller.displayedPerson = person;
controller.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]];
controller.personViewDelegate = self;
[peoplePicker pushViewController:controller animated:YES];
[controller release];
return NO;
}
-(BOOL)personViewController:(ABPersonViewController *)personViewController
shouldPerformDefaultActionForPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifierForValue
{
ABMutableMultiValueRef multiEmail = ABRecordCopyValue(person, property);
NSString *emailAddress = (NSString *) ABMultiValueCopyValueAtIndex(multiEmail, identifierForValue);
NSLog(#"strEmail %#",emailAddress);
ABPeoplePickerNavigationController *peoplePicker = (ABPeoplePickerNavigationController *)personViewController.navigationController;
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
return NO;
}

Selecting events using Kal calendar

I'm adding a calendar view to my app using Kal Calendar but am having problems implementing a didSelectRowAtIndexPath method on the event list. I would like to push a view controller when the user selects an event for any given day. I've tried putting the method in "KalView.m", "KalViewController.m", and "KalDataSource.m", but none are recognized. Where is the appropriate place to call such a method?
I had similar issue once, Here is how I implemented it.
#import <MTDates/NSDate+MTDates.h>
#import <ObjectiveSugar/ObjectiveSugar.h>
#import <UIImageView+WebCache.h>
#import "EventsViewController.h"
#import "EventDetailsViewController.h"
#import "EventCell.h"
#import "Event.h"
#import "KalViewController.h"
#import "CalendarViewController.h"
#implementation EventsViewController
- (id)initWithEvents:(NSArray *)_events {
self = [super init];
events = _events;
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self listUpdated];
[self addCalendarView];
}
- (void)listUpdated
{
NSMutableArray *allEvents = [NSMutableArray array];
[allEvents addObjectsFromArray:events];
NSArray *sortedArray = [allEvents sortedArrayUsingComparator:^NSComparisonResult(Event *obj1, Event *obj2) {
return [obj1.date compare:obj2.date];
}];
_allEvents = sortedArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_allEvents count];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//custom table cell (EventCell is a view I'm initialising my rows with)
Event *event = _allEvents[indexPath.row];
NSString *reuseIdentifier = [NSString stringWithFormat:#"Cell%#%#", event.venue.identifier, event.identifier];
EventCell *cell = (EventCell*) [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [EventCell createDetailedCellWithReuseIdentifier:reuseIdentifier];
cell.nameLabel.text = event.name;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Event *event = _allEvents[indexPath.row];
EventDetailsViewController *controller = [[EventDetailsViewController alloc] initWithEvent:event];
[self.navigationController pushViewController:controller animated:YES];
}
- (void) addCalendarView{
_calenderView = [[KalViewController alloc] initWithSelectedDate:[NSDate date]];
[[self.view viewWithTag:2] addSubview:_calenderView.view]; /* depends on your requirements*/
_calenderView.view.tag = 200; /* not necessary */
[_calenderView.view setFrame:self.view.bounds];
_calenderView.dataSource = self;
_calenderView.delegate = self;
}
- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
//NSLog(#"%#, %#", fromDate, toDate);
// filter and pass the array to the events tableview
[self filterByDateSelected:toDate];
}
-(void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
/** when selecting a different month **/
}
- (void)removeAllItems
{
NSLog(#"Items Removed");
// remove all the previous items from the tableview
}
-(void) filterByDateSelected: (NSDate *)selectedDate
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSString *theDate = [dateFormat stringFromDate:selectedDate];
NSDate *_date = [NSDate dateFromString:theDate usingFormat:#"yyyy-MM-dd"];
// filter table by selectedDate
NSArray *_dateFilteredEvents = _allEvents;
_dateFilteredEvents = [_allEvents filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Event *evaluatedEvent, NSDictionary *bindings) {
if ([evaluatedEvent.date isEqualToDate:_date]) {
return YES;
}
return NO;
}]];
NSLog(#"%#", _dateFilteredEvents);
// uncomment the following line if you want to display list in another controller
//[self showEventsByDate:_dateFilteredEvents];
// or else refresh table after updating the list
_allEvents = _dateFilteredEvents;
[_tableView reloadData];
}
- (void) showEventsByDate:(NSArray*)events
{
if (events.count > 0) {
CalendarViewController *_controller = [[CalendarViewController alloc] initWithEventArray:events];
[self.navigationController pushViewController:_controller animated:YES];
}
}
- (void) removeCalenderView{
[_calenderView.view removeFromSuperview];
}
#end

potential leak at CFStringRef while getting phone number from contacts

I am getting phone number from the address book for that i am using this code.
- (IBAction)contacts {
ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
peoplePickerController.peoplePickerDelegate = self;
[self presentModalViewController:peoplePickerController animated:NO];
[peoplePickerController release];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty) {
ABMultiValueRef phonenumbers = ABRecordCopyValue(person, property);
CFStringRef phonenumberselected = ABMultiValueCopyValueAtIndex(phonenumbers, identifier);
NSString *aNSString = (NSString *)phonenumberselected;
if ([share_toadd length] == 0) {
[share_toadd appendString:aNSString];
}
else {
[share_toadd appendString:#","];
[share_toadd appendString:aNSString];
}
share_textfield.text = share_toadd;
// Return to the main view controller.
[ self dismissModalViewControllerAnimated:YES ];
return NO;
[share_textfield release];
}
return YES;
}
but i am getting potential leak at CFStringRef
Potential leak of an object allocated on line 1126
Call to function 'ABRecordCopyValue' returns a Core Foundation object with a +1 retain count (owning reference)
Object allocated on line 1126 is no longer referenced after this point and has a retain count of +1 (object leaked)
can any one pls help me.
how can i resolve it.
You should call CFRelease(phonenumbersselected) when you're done using the object.

ABPeoplePickerNavigationController actually executing

i have the contact list showing up perfectly in the simulator. it takes the phone number and places it in the text box. so i decided to try it on my iphone and it actually executes the thing i tap on. it calls the number instead of putting the number ito the textbox. heres the code:
- (IBAction) adressBook: (id) sender {
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
/*
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
num.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
//[self dismissModalViewControllerAnimated:YES];
*/
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
NSLog(#"inbool");
ABMultiValueRef phonePro = ABRecordCopyValue(person, property);
int idx = ABMultiValueGetIndexForIdentifier(phonePro, identifier);
num.text = (NSString*)ABMultiValueCopyValueAtIndex(phonePro, idx);
[self dismissModalViewControllerAnimated:YES];
/*
ABMultiValueRef multi = ABRecordCopyValue(person, property);
num.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, identifier);
*/
return YES;
}
and sorry if not formatted correctly, new to stackoverflow.
Your peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: needs to return NO so that the phone does not perform it's default action. You then close the picker yourself.
-(BOOL) peoplePickerNavigationController: (ABPeoplePickerNavigationController *) peoplePicker
shouldContinueAfterSelectingPerson: (ABRecordRef) person
property: (ABPropertyID) property
identifier: (ABMultiValueIdentifier) identifier
{
NSLog(#"inbool");
ABMultiValueRef phonePro = ABRecordCopyValue(person, property);
int idx = ABMultiValueGetIndexForIdentifier(phonePro, identifier);
num.text = (NSString)ABMultiValueCopyValueAtIndex(phonePro, idx);
[peoplePicker dismissModalViewControllerAnimated: YES];
return NO;
}

Using Cocoa Touch Tutorial: Extract Address Book Address Values on iPhone OS

I've followed the following tutorial, in the simulator it works great, however on my phone when select the address, Google maps launchs, I think I've fried my brain on this. I am using this in conjunction to a NavBarContolloer Any help would be great.
Taken from: Cocoa Touch Tutorial: Extract Address Book Address Values on iPhone OS
Here's the code:
#import "RootViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#implementation ThirdViewController
#synthesize fourthViewController;
#synthesize firstName;
#synthesize lastName;
#synthesize addressLabel;
-(IBAction)switchPage:(id)sender
{
if(self.fourthViewController == nil)
{
FourthViewController *fourthView = [[FourthViewController alloc]
initWithNibName:#"FourthView" bundle:[NSBundle mainBundle]];
self.fourthViewController = fourthView;
[fourthView release];
}
[self.navigationController pushViewController:self.fourthViewController animated:YES];
}
-(IBAction)getContact {
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
// setting the first name
firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// setting the last name
lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// setting the street name
//ABMultiValueRef street = ABRecordCopyValue(person, kABPersonAddressProperty);
// street.text = (NSString *)ABRecordCopyValue(person, kABPersonAddressStreetKey);
// setting the number
/*
this function will set the first number it finds
if you do not set a number for a contact it will probably
crash
*/
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
number.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
// remove the controller
//[self dismissModalViewControllerAnimated:YES];
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
// Only inspect the value if it's an address.
if (property == kABPersonAddressProperty) {
/*
* Set up an ABMultiValue to hold the address values; copy from address
* book record.
*/
ABMultiValueRef multi = ABRecordCopyValue(person, property);
// Set up an NSArray and copy the values in.
NSArray *theArray = [(id)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
// Figure out which values we want and store the index.
const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi, identifier);
// Set up an NSDictionary to hold the contents of the array.
NSDictionary *theDict = [theArray objectAtIndex:theIndex];
// Set up NSStrings to hold keys and values. First, how many are there?
const NSUInteger theCount = [theDict count];
NSString *keys[theCount];
NSString *values[theCount];
// Get the keys and values from the CFDictionary. Note that because
// we're using the "GetKeysAndValues" function, you don't need to
// release keys or values. It's the "Get Rule" and only applies to
// CoreFoundation objects.
[theDict getObjects:values andKeys:keys];
// Set the address label's text.
NSString *address;
address = [NSString stringWithFormat:#"%#, %#, %#, %# %#",
[theDict objectForKey:(NSString *)kABPersonAddressStreetKey],
[theDict objectForKey:(NSString *)kABPersonAddressCityKey],
[theDict objectForKey:(NSString *)kABPersonAddressStateKey],
[theDict objectForKey:(NSString *)kABPersonAddressZIPKey],
[theDict objectForKey:(NSString *)kABPersonAddressCountryKey]];
self.addressLabel.text = address;
// Memory management.
[theDict release];
// Return to the main view controller.
[ self dismissModalViewControllerAnimated:YES ];
// return Yes;
}
// If they didn't pick an address, return YES here to keep going.
return YES;
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
In
-[ABPeoplePickerNavigationControllerDelegate peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:]
you need to return NO in order not to launch Google Maps. Returning YES would continue with the default action, which on the device is launching Google Maps.