我正在开发一个应用程序,该应用程序允许用户从电话的通讯录中选择联系人,并在UITableView中显示该联系人。我正在使用以下代码来获取联系人;

-(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);

}


现在的任务是,一旦用户从通讯簿中找到一个联系人,ABPeoplePickerNavigator就应该被取消,而MFMessageComposeViewController应该立即呈现一个默认消息和所选择的联系人号码。因此,应用程序的用户应将消息发送到所选择的联系人号码。我已经将以下代码用于消息撰写视图控制器;

-(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];

   }

}


现在,请帮助我在哪里调用此方法,以便在ABPeoplePickerNavigationController关闭后立即显示MFMessageComposeViewController。

最佳答案

使用新功能dismissViewControllerAnimated:完成:因此在完成调用后,请发送短信,因为我已经编辑了您的代码。

- (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;
}

10-07 14:11
查看更多