首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS中的关闭通讯簿

iOS中的关闭通讯簿
EN

Stack Overflow用户
提问于 2014-10-19 01:16:46
回答 1查看 655关注 0票数 0

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

代码语言:javascript
运行
复制
- (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];
    }

我想弄清楚的是,如果我想让用户打开地址簿并向其中一个联系人发送电子邮件,那么在邮件发送后,用户必须重新打开我希望他们返回应用程序的app...and。下面是打开通讯簿、查看联系人、发送电子邮件/打电话/向联系人发送消息的代码。

代码语言:javascript
运行
复制
- (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的建议之后,编辑,我的代码如下所示:

代码语言:javascript
运行
复制
- (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];
}

我可以选择一个联系人或打电话或电子邮件,但仍然必须手动返回应用程序后,完成这些行动之一。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-19 01:54:13

您的shouldContinueAfterSelectingPerson:property:identifier:正在返回YES,它告诉用户选择器您希望iOS处理电子邮件的发送(也就是说,它将离开您的应用程序)。如果您不想离开您的应用程序,请让这个函数返回NO,然后在适当的ABPeoplePickerNavigationControllerDelegate函数中显示您自己的MFMailComposeViewController (如前面所示)。然后,您可以删除联系人,并开始发送电子邮件,同时停留在您的应用程序。

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

代码语言:javascript
运行
复制
#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增强:

代码语言:javascript
运行
复制
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];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26446275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档