我正在处理一个应用程序,该应用程序允许用户从电话的地址簿中选择联系人,并在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就会出现。
答案 0 :(得分: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;
}
答案 1 :(得分:0)
当您解散时,将地址控制器的动画设置为“否”
[self dismissViewControllerAnimated:NO completion:nil];
答案 2 :(得分:0)
尝试
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;
呼叫
-(void)sendSMS
in dismissViewControllerAnimated:completion:completion handler。
[self dismissViewControllerAnimated:YES completion:{[self sendSMS]}];
希望这会有所帮助。
答案 3 :(得分:0)
MFMessageComposeViewController * controller;
ABPeoplePickerNavigationController * picker;
并且在.m中就这样做了
-(void)sendSMS
中的{ //其他代码
[picker presentViewController:controller animated:YES completion:nil];
}