简体   繁体   English

在iOS中关闭通讯录

[英]Closing Address Book in iOS

I know how to open up and send an email in iOS using MFMailComposeViewController . 我知道如何使用MFMailComposeViewController在iOS中打开并发送电子邮件。 The code below will let the user write and send an email, and then go straight back to the app. 下面的代码将允许用户编写和发送电子邮件,然后直接返回到应用程序。

- (IBAction)sendEmail:(id)sender {
        // Email Subject
        NSString *emailTitle = @"Test";
        // Email Content
        NSString *messageBody = @"Test";
        // To address
        NSArray *toRecipents = [NSArray arrayWithObject:@"te@gmail.com"];


        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
        mc.mailComposeDelegate = self;
        [mc setSubject:emailTitle];
        [mc setMessageBody:messageBody isHTML:NO];
        [mc setToRecipients:toRecipents];

        // Present mail view controller on screen
        [self presentViewController:mc animated:YES completion:NULL];

    }

    - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved");
                break;
            case MFMailComposeResultSent:
                NSLog(@"Mail sent");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail sent failure: %@", [error localizedDescription]);
                break;
            default:
                break;
        }

        // Close the Mail Interface
        [self dismissViewControllerAnimated:YES completion:NULL];
    }

What I'm struggling to figure out is if I want to have the user open up the address book and send an email to one of there contacts, after the email is sent, the user has to re-open the app...and I'd like them to return to the app automatically. 我要弄清楚的是,是否要让用户打开通讯录并向其中一个联系人发送电子邮件,在发送电子邮件之后,用户必须重新打开应用程序...我希望他们自动返回到应用程序。 Below is the code for opening up the address book, viewing a contact, and either sending an email/making a call/sending a message to that contact. 以下是打开通讯录,查看联系人以及发送电子邮件/拨打电话/向该联系人发送消息的代码。

- (IBAction)openContacts:(id)sender {
    ABPeoplePickerNavigationController *peoplePicker =
    [[ABPeoplePickerNavigationController alloc] init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePicker animated:YES];
}


- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
//    [self dismissModalViewControllerAnimated:YES];
    return YES;
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier
{
    return YES;
}

- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)picker
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

Does anyone know to have the user return straight back to the app after opening the address book? 有谁知道让用户在打开地址簿后直接回到应用程序吗? Thanks! 谢谢!

EDIT After implementing @Rob's suggestion, my code looks like this: 编辑实施@Rob的建议后,我的代码如下所示:

- (IBAction)openContacts:(id)sender {
    ABPeoplePickerNavigationController *peoplePicker =
    [[ABPeoplePickerNavigationController alloc] init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePicker animated:YES];
}


- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    return YES;
}


- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
                         didSelectPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {
    if (property == kABPersonEmailProperty) {
        ABMultiValueRef emails = ABRecordCopyValue(person, property);
        CFIndex index = ABMultiValueGetIndexForIdentifier(emails, identifier);
        NSString *email = CFBridgingRelease(ABMultiValueCopyValueAtIndex(emails, index));
        CFRelease(emails);

        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] init];
        [composeViewController setToRecipients:@[email]];
        composeViewController.mailComposeDelegate = self;

        [peoplePicker dismissViewControllerAnimated:NO completion:^{
            [self presentViewController:composeViewController animated:YES completion:nil];
        }];
    } else {
        [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    }
}


- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

I'm able to select a contact and either make a phone call or email, but still have to return to the app manually after completing one of these actions. 我可以选择一个联系人,然后拨打电话或发送电子邮件,但完成这些操作之一后,仍然必须手动返回到该应用程序。

Your shouldContinueAfterSelectingPerson:property:identifier: is returning YES , which tells the people picker that you want iOS to handle the sending of the email (ie, it will leave your app). 您的shouldContinueAfterSelectingPerson:property:identifier:返回YES ,这告诉人员选择器您希望iOS处理电子邮件的发送(即它将离开您的应用程序)。 If you don't want to leave your app, have this function return NO and then present your own MFMailComposeViewController (like you showed earlier) in the appropriate ABPeoplePickerNavigationControllerDelegate function. 如果您不想离开应用程序,请让此函数返回NO ,然后在适当的ABPeoplePickerNavigationControllerDelegate函数中展示您自己的MFMailComposeViewController (如您先前显示的)。 Then you can pull up contacts and initiate the sending of an email while staying within your app. 然后,您可以在不影响应用程序的情况下拉起联系人并开始发送电子邮件。

For example, you might do something like: 例如,您可以执行以下操作:

#pragma mark - ABPeoplePickerNavigationControllerDelegate

// for iOS versions before 8.0

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {
    [self peoplePickerNavigationController:peoplePicker
                           didSelectPerson:person
                                  property:property
                                identifier:identifier];

    return NO;
}

// for iOS 8+

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
                         didSelectPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {
    if (property == kABPersonEmailProperty) {
        ABMultiValueRef emails = ABRecordCopyValue(person, property);
        CFIndex index = ABMultiValueGetIndexForIdentifier(emails, identifier);
        NSString *email = CFBridgingRelease(ABMultiValueCopyValueAtIndex(emails, index));
        CFRelease(emails);

        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] init];
        [composeViewController setToRecipients:@[email]];
        composeViewController.mailComposeDelegate = self;

        [peoplePicker dismissViewControllerAnimated:NO completion:^{
            [self presentViewController:composeViewController animated:YES completion:nil];
        }];
    } else {
        [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

Note, I implement the logic in the iOS 8 routine, didSelectPerson , but call it from shouldContinueAfterSelectingPerson for backward compatibility. 注意,我在iOS 8例程didSelectPerson实现了该逻辑,但是从shouldContinueAfterSelectingPerson调用它是为了向后兼容。


Unrelated to your question, but if using people picker to select email address, in iOS 8, you can keep the user from selecting contacts with no email address, as well as when showing the contact, only show the email address(es). 与您的问题无关,但是如果使用人员选择器选择电子邮件地址,则在iOS 8中,可以使用户避免选择没有电子邮件地址的联系人,以及在显示联系人时仅显示电子邮件地址。 Just a few nice iOS 8 enhancements: 仅有一些不错的iOS 8增强功能:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;

// if you don't want user to select contacts with no email address

if ([picker respondsToSelector:@selector(setPredicateForEnablingPerson:)]) {
    picker.predicateForEnablingPerson = [NSPredicate predicateWithFormat:@"emailAddresses.@count > 0"];
}

// if you don't want to show phone numbers, mailing addresses, etc., you can show just email addresses in iOS 8

if ([picker respondsToSelector:@selector(setDisplayedProperties:)]) {
    picker.displayedProperties = @[@(kABPersonEmailProperty)];
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM