【问题标题】:Closing Address Book in iOS在 iOS 中关闭通讯录
【发布时间】:2014-10-19 23:36:00
【问题描述】:

我知道如何在 iOS 中使用 MFMailComposeViewController 打开和发送电子邮件。下面的代码将让用户编写和发送电子邮件,然后直接返回应用程序。

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

我想弄清楚的是,如果我想让用户打开通讯簿并向其中一个联系人发送电子邮件,则在发送电子邮件后,用户必须重新打开应用程序。 ..我希望他们自动返回应用程序。以下是用于打开通讯录、查看联系人以及向该联系人发送电子邮件/拨打电话/发送消息的代码。

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

有谁知道让用户在打开通讯录后直接返回应用程序?谢谢!

编辑 执行@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];
}

我可以选择联系人并拨打电话或发送电子邮件,但在完成其中一项操作后仍需要手动返回应用程序。

【问题讨论】:

  • 谢谢,刚刚解决了这个问题!

标签: ios objective-c mfmailcomposeviewcontroller abpeoplepickerview


【解决方案1】:

您的 shouldContinueAfterSelectingPerson:property:identifier: 正在返回 YES,它告诉人员选择器您希望 iOS 处理电子邮件的发送(即,它将离开您的应用程序)。如果您不想离开您的应用程序,请让此函数返回 NO,然后在适当的 ABPeoplePickerNavigationControllerDelegate 函数中显示您自己的 MFMailComposeViewController(如您之前所示)。然后,您可以在您的应用程序中提取联系人并开始发送电子邮件。

例如,您可能会执行以下操作:

#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];
}

注意,我在 iOS 8 例程 didSelectPerson 中实现了逻辑,但为了向后兼容,从 shouldContinueAfterSelectingPerson 调用它。


与您的问题无关,但如果使用人员选择器选择电子邮件地址,在 iOS 8 中,您可以阻止用户选择没有电子邮件地址的联系人,以及在显示联系人时,仅显示电子邮件地址(es )。只是一些不错的 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];

【讨论】:

  • 您好 Rob,感谢您的回答,这是有道理的。我将返回值从 YES 更改为 NO。我唯一感到困惑的是在哪里放置代码以呈现MFMailComposeViewController 我应该把它放在- (BOOL)peoplePickerNavigationController: 下面的新函数中吗?
  • 把它放在shouldContinueAfterSelectingPerson 方法中(也可能是didSelectPerson 例程)。请参阅修改后的答案中的代码示例。
  • 您好 Rob,感谢您抽出宝贵时间写出如此详尽的答案,非常感谢。我已经实施了你的建议(除非我误解了什么),并将新代码放在我原来的问题中的 EDIT 下。我可以选择联系人并拨打电话或发送电子邮件,但在完成其中一项操作后仍需要手动返回应用程序。
  • 嗨@Rob,谢谢你的观点,否则不会知道。对于2,我在-(BOOL)peoplePickerNavigationController里面放了一个NSLog,但是Xcode给了我一个错误,说代码永远不会执行。 - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { return YES; NSLog(@"代码跑到这里了!"); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多