2

我正在开发一个应用程序,该应用程序允许用户从手机的通讯录中选择一个联系人并在 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];

   }

}

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

4

4 回答 4

1

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

- (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;
}
于 2013-01-29T06:56:19.947 回答
0

关闭时将地址控制器的动画设置为否

    [self dismissViewControllerAnimated:NO completion:nil];
于 2013-01-29T06:53:55.423 回答
0

在 .h 文件中

MFMessageComposeViewController *控制器;

ABPeoplePickerNavigationController *picker;

在 .m 中这样做

in -(void)sendSMS { //其他代码

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

}

于 2013-05-31T12:34:19.990 回答
0

试穿

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

称呼

-(void)sendSMS

在dismissViewControllerAnimated:completion: 完成处理程序中。

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

希望这会有所帮助。

于 2013-01-29T07:01:04.870 回答