donot open ABPersonViewController - iphone

i am trying to open ABPersonViewController at table delegate method (DidSelectRowAtIndex). but when i tap on one of my contact person in table view it shows "obj msg send". help me
here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Fetch the address book
if ((people != nil) && [people count])
{
ABAddressBookRef addressBook = ABAddressBookCreate();
//ABPersonViewController *personController = [[ABPersonViewController alloc] initWithNibName:#"ABPersonViewController" bundle:nil];
ABRecordRef person = (ABRecordRef)[people objectAtIndex:indexPath.row];
ABPersonViewController *personController = [[ABPersonViewController alloc] init];
personController.addressBook = addressBook;
personController.personViewDelegate = self;
personController.displayedPerson = person;
personController.allowsEditing = YES;
//navigationController = [[UINavigationController alloc] init] ;
[self presentModalViewController:personController animated:YES];
//[self.navigationController pushViewController:personController animated:YES];
[personController release];
} else
{
// Show an alert if "KETAN" is not in Contacts
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Could not find naina in the Contacts application"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[people release];
}

You are doing an unnecessary release here, CFRelease(person);. You are just getting the value directly from an array so you shouldn't release it. Moreover, the ABPersonViewController object doesn't retain the person object assigned to displayedPerson so this results in an error when it tries to access the object which has been released.

If you want the personview to open in edit mode, in addition to allowsEditing = YES, you need to specify setEditing:YES:
[personController setEditing:YES animated:NO];

Use this line instead of that in your code,
ABPersonViewController *personController = [[ABPersonViewController alloc] initWithNibName:#"ABPersonViewController" bundle:nil];

Related

Issue in Pushing ABPersonViewController

i am having trouble in pushing ABpersonViewController when the name is from user added then everything works perfectly but when name is from default simulator entries then its not working i will explain in detail in code
-(void)showPersonViewController:(NSString *)name
{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *string = name;
NSLog(#"%#",string);
CFStringRef cfstringRef = (CFStringRef)string;
NSArray *peoplee = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, cfstringRef);
NSLog(#"%#",peoplee);
// 1ST QUESTION when contact is from defalut contact nslog is null but when from user added then it has value I dont understand why this is happening
if ((peoplee != nil) && [peoplee count])
{
ABRecordRef person = (ABRecordRef)[peoplee objectAtIndex:0];
ABPersonViewController *picker = [[ABPersonViewController alloc] init];
picker.personViewDelegate = self;
picker.displayedPerson = person;
picker.allowsEditing = YES;
[self.navigationController pushViewController:picker animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Could not find Appleseed in the Contacts application"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
}
CFRelease(addressBook);
}
I replaced your code of converting NSString To CFStringRef :
This is if you are using ARC:
CFStringRef cfstringRef = (__bridge_retained CFStringRef)string;
But it seems you are not, so for Non ARC:
CFStringRef cfstringRef = (CFStringRef)string;
-(void)showPersonViewController
{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *string = #"Appleseed";
CFStringRef cfstringRef = (CFStringRef)string;
NSArray *peoplee = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, cfstringRef);
NSLog(#"%#",peoplee); // does not print null if you have Appleseed as your contact
if ((peoplee != nil) && [peoplee count])
{
ABRecordRef person = (ABRecordRef)[peoplee objectAtIndex:0];
ABPersonViewController *picker = [[ABPersonViewController alloc] init];
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];
}
CFRelease(addressBook);
}

App crashing on changing views after login

Good morning!
I am very new to iPhone/iPad programming
My app is crashing (signal SIGABRT) after trying to change views after a Login
-(void)checkLogin {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"LoginData" ofType:#"txt"];
NSString *myText = [NSString stringWithContentsOfFile:filePath];
//Parse lines into an NSArray;
NSArray *results= [myText componentsSeparatedByString:#"\n"];// Assumes Mac line end return
if([txtUsername.text isEqualToString: [results objectAtIndex:0]]&& [txtPassword.text isEqualToString: [results objectAtIndex:1]])
{
Clients * clients = [[Clients alloc] initWithNibName:#"clients" bundle:nil];
[self presentModalViewController:clients animated:YES];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Login"
message:#"Wrong credentials"
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles:nil];
[alert show];
}
}
#end
The app is crashing at this line
[self presentModalViewController:clients animated:YES];
Thank you very much!
Clients * clients = [[AboutUs alloc] initWithNibName:#"clients" bundle:nil];
[self presentModalViewController:clients animated:YES];
I think the first line is where your app gets stuck. turn it into:
Clients *clients = [[Clients alloc] initWithNibName:#"clients" bundle:nil];
[self presentModalViewController:clients animated:YES];
This code should work, but In your question, you put a certain aboutUs in your code.
I don't know what you want to do with this from you question, but if you want to put this AboutUs controller inside your Clients controller, you should use something like:
Clients *clients = [[Clients alloc] initWithRootViewController:aboutUs];
hope this helps you
Have you tried doing it like this:
Clients *myClients = [[Clients alloc] init];
[self presentViewController:clients animated:YES completion:^(void) {
}];
It should work properly. If it still crashes, something in the Clients class is going wrong.

Three else-if statements [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Hi I am trying to have 4 buttons and each of them have different alerts when pressed. I am running into an issue, I had 3 buttons and I decided to add a "Rate My App" Button but now it isnt' working, please help me. (By the way the error is Expexted Exspression)(Org.=Organization (Fixed in App), Email- Real email in app,
#define TAG_Band 1
#define TAG_DEV 2
#define TAG_EDEV 3
#define TAG_RATE 4
#interface Org.ContactInfo () <MFMailComposeViewControllerDelegate>
#end
#implementation Org.ContactInfo:UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Contacts";
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)ContactBand:(id)sender;{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Contact the Band" message:#"Contact the Org. or go to their website!" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Visit the Org. Website",#"E-Mail The Org. President", #"E-Mail The Org. Treasurer", nil];
alert.tag = TAG_Band;
[alert show];
}
-(IBAction)ContactDev:(id)sender;{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Contact Me" message:#"Contact Me on Features you would like me to consider! I will do my Best to look at all of the Suggestions!" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Visit My Website",#"E-Mail Me!", nil];
alert.tag = TAG_DEV;
[alert show];
}
-(IBAction)RateMyApp:(id)sender;{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Rate My App" message:#"When Your Reviewing my App, Please remember that this app was at no cost to the Mighty Mustang Band." delegate:self cancelButtonTitle:#"Not Right Now" otherButtonTitles:#"Rate My App!!", nil];
alert.tag = TAG_RATE;
[alert show];
}
-(IBAction)AppInfo:(id)sender;{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Version 1.0" message:#"More Updates Coming Soon. Please Feel Free to E-Mail me on features that you would like me to consider" delegate:self cancelButtonTitle:#"Not Right Now" otherButtonTitles:#"Email-Me", nil];
alert.tag = TAG_EDEV;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == TAG_Band){
if (buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://URL.org"]];
}
else if (buttonIndex==2){
//Subject
NSString *emailTitle = #"";
//Recipients
NSString *emailBody=#"Org. President";
NSArray *toRecipients = [NSArray arrayWithObject:#"Person#Org.org"];
MFMailComposeViewController *mc=[[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setSubject:emailBody];
[mc setToRecipients:toRecipients];
[self presentViewController:mc animated:YES completion:NULL];
}
else if (buttonIndex==3){
//Subject
NSString *emailTitle = #"";
//Recipients
NSString *emailBody=#"Org. Treasurer, ";
NSArray *toRecipients = [NSArray arrayWithObject:#"Person#Org.org"];
MFMailComposeViewController *mc=[[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setSubject:emailBody];
[mc setToRecipients:toRecipients];
[self presentViewController:mc animated:YES completion:NULL];
}
}
else if (alertView.tag == TAG_DEV){
if (buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://stepheniosdeveloper.wordpress.com"]];
}
else if (buttonIndex==2){
NSString *emailTitle = #"";
//Recipients
NSString *emailBody=#"";
NSArray *toRecipients = [NSArray arrayWithObject:#"Email#gmail.com"];
MFMailComposeViewController *mc=[[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setSubject:emailBody];
[mc setToRecipients:toRecipients];
[self presentViewController:mc animated:YES completion:NULL];
}
}
else if (alertView.tag == TAG_EDEV);{
if (buttonIndex==1){
NSString *emailTitle = #"";
//Recipients
NSString *emailBody=#"";
NSArray *toRecipients = [NSArray arrayWithObject:#"Email#gmail.com"];
MFMailComposeViewController *mc=[[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setSubject:emailBody];
[mc setToRecipients:toRecipients];
[self presentViewController:mc animated:YES completion:NULL];
}
}
else if (alertView.tag == TAG_RATE);{ //Expected Expression
if (buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://itunes.apple.com/us/app/Org/id607257427?ls=1&mt=8"]];
}
}
}
It worked Before I added the last on (TAG_RATE) but now I don't understand why it isn't working anymore. Please Help.
There is a typo in your code: you have extra ; after the last two else if conditions:
else if (alertView.tag == TAG_EDEV)/*;*/{ // extra `;`
//...
}
else if (alertView.tag == TAG_RATE)/*;*/{ // extra `;` //Expected Expression
// ...
}

How to edit the existing contacts programmatically in iPhone

I want to edit the contact list programatically.is there any API's available for this.....
-(void)showPersonViewController:(NSString *)nameInContact
{
// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// Search for the person in the address book
NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, CFSTR(nameInContact));
// Display the 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];
}
else
{
// Show an alert if the person is not in Contacts
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:[NSString stringWithFormat:#"Could not find %# in the Contacts application", nameInContact]
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[people release];
CFRelease(addressBook);
}
You can access the contacts programmatically and edit them using ABAddressBook.framework.
You can find documentation here:
http://developer.apple.com/library/mac/#documentation/userexperience/Reference/AddressBook/Classes/ABAddressBook_Class/Reference/Reference.html
and programming guide here:
http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html

adding all person in form of array in ABRecordRef

how can i add the people from the contacts to 'people'- array then to abrecordref.??? Actually i want all the contacts in tableview and can able to edit individual record. here is my code:
-(void)showPersonViewController
{
// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// Search for the person named "rubal" in the address book
NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, CFSTR("naina"));
// Display "KETAN" 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];
}
else
{
// Show an alert if "KETAN" is not in Contacts
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Could not find naina in the Contacts application"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[people release];
CFRelease(addressBook);
}
instead of only 'naina' i want all record in table view and edit individually
Look at ABPerson Class Reference. There is a list of other copy functions, including the one you seek, ABAddressBookCopyArrayOfAllPeople(ABAddressBookRef ab)
comment out your function and add this one and see if it works
-(BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[peoplePicker dismissModalViewControllerAnimated:NO];
ABPersonViewController *picker = [[ABPersonViewController alloc] init];
picker.personViewDelegate = self;
picker.displayedPerson = person;
// Allow users to edit the person’s information
picker.allowsEditing = YES;
[picker setValue:[NSNumber numberWithBool:YES] forKey:#"allowsDeletion"];
[self.navigationController pushViewController:picker animated:YES];
return YES;
}