user1940888
user1940888

Reputation: 475

Presenting MFMessageComposeViewController as soon as the user selects a contact from ABPeoplePickerNavigationController

Im working on an app which allows the user to select a contact from phone's address book and displays that contact in the UITableView. Im using following code to pick up the contact;

-(IBAction)pickPhoneContacts:(id)sender //opens phone's address book
{
   ABPeoplePickerNavigationController *picker =

   [[ABPeoplePickerNavigationController alloc] init];

   picker.peoplePickerDelegate = self;

   [self presentViewController:picker animated:YES completion:nil];

}

  //called when user presses the cancel button in the Address book view controller
  - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController*)peoplePicker

 {
    [self dismissViewControllerAnimated:YES completion:nil];
 }


   //called when user pics up a contact from the phone's address book

   - (BOOL)peoplePickerNavigationController:

  (ABPeoplePickerNavigationController *)peoplePicker

  shouldContinueAfterSelectingPerson:(ABRecordRef)person {


  [self displayPerson:person]; //calls displayPerson:(ABRecordRef)person to show contact's information in the app

  [self dismissViewControllerAnimated:YES completion:NULL];

   return NO;
}


//called when the user selects the property of the contact. This method will not be called in the  app but included to complete the implementation of the protocol


- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)

peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

 property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
 {
   return NO;
 }

   //Displays the contact name and the contact's primary number in the app's text fields (add contact view controller)
 - (void)displayPerson:(ABRecordRef)person
 {
    NSString* name = (__bridge_transferNSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty); //Extracts the contact's first name from address book & assigns it to a string value


  self.contactNameTextFiled.text = name;

  NSString* phone = nil;

  //Extracts the first phone number among multiple contact numbers from address book & assigns it to a string value
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);

if (ABMultiValueGetCount(phoneNumbers) > 0)
{
    phone = (__bridge_transfer NSString*)

    ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

}
else
{
    phone = @"[None]";
}

self.primaryNumberTextField.text = phone;

CFRelease(phoneNumbers);

}

Now the task is, as soon as the user pics up a contact from the address book, the ABPeoplePickerNavigator should dismiss and immediately MFMessageComposeViewController should present with a default message and the picked contact number. So that the user of the app should send a message to the picked contact number. I have used following code for message compose view controller;

-(void)sendSMS
{
   MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
   if([MFMessageComposeViewController canSendText])
   {
      controller.body = @"Test Message";
      controller.recipients = [NSArray arrayWithObjects:@"111222333", nil];
      controller.messageComposeDelegate = self;
       [self presentViewController:controller animated:YES completion:nil];

   }

}

Now please help me where should I call this method so that the MFMessageComposeViewController should present as soon as the ABPeoplePickerNavigationController dismiss.

Upvotes: 2

Views: 1047

Answers (4)

Nagendra Tripathi
Nagendra Tripathi

Reputation: 923

in .h file

MFMessageComposeViewController *controller ;

ABPeoplePickerNavigationController *picker;

and in .m just do this

in -(void)sendSMS { //other code

 [picker presentViewController:controller animated:YES completion:nil];

}

Upvotes: 0

Aditya Mathur
Aditya Mathur

Reputation: 1185

Try in

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;

call

-(void)sendSMS

in dismissViewControllerAnimated:completion: completion handler.

[self dismissViewControllerAnimated:YES completion:{[self sendSMS]}];

Hopes this helps.

Upvotes: 0

Bhupendra
Bhupendra

Reputation: 2545

Use new function dismissViewControllerAnimated: completion: so after completion call send sms as i have edited your code.

- (BOOL)peoplePickerNavigationController:

  (ABPeoplePickerNavigationController *)peoplePicker

  shouldContinueAfterSelectingPerson:(ABRecordRef)person {


  [self displayPerson:person]; //calls displayPerson:(ABRecordRef)person to show contact's information in the app

//  [self dismissViewControllerAnimated:YES completion:NULL];
    [self dismissViewControllerAnimated:YES completion:^{
        [self sendSMS];
    }];

   return NO;
}

Upvotes: 1

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

Set animation of your adresscontroller to No when you are dismissing it

    [self dismissViewControllerAnimated:NO completion:nil];

Upvotes: 0

Related Questions