如何让用户选择邮件收件人?

时间:2010-11-03 16:17:32

标签: iphone objective-c cocoa-touch

在我的应用程序中,我使用下面的代码来处理从iphone发送的消息我想知道如何让他在联系我们之间做出选择并告诉朋友像其他应用程序一样如何才能解决这个问题?

代码:

-(IBAction)sendEmail {
    //Create a new mailComposer object and set the mailComposeDelegate (required).
    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;

    if ([MFMailComposeViewController canSendMail]) {

        //Set Fields such as Subject, recipients, and message body.
        [mailComposer setToRecipients:[NSArray arrayWithObjects:@"info@Myweb.com",nil]];
        [mailComposer setSubject:@"App Feadback"];

        //Present the mail view controller
        [self presentModalViewController:mailComposer animated:YES];
    }

    //release the mailComposer as it is now being managed as the UIViewControllers modalViewController.
    [mailComposer release];
}

//MFMailComposeControllerDelegate method that handles success or failure
//and dismisses the mailComposer
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    [self dismissModalViewControllerAnimated:YES];

    if (result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Failed" message:@"Your message failed to send" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

1 个答案:

答案 0 :(得分:0)

对于“联系我们”,您必须将邮件地址预填充到mailComposer.setToRecipient。就像你做的那样。

要告诉朋友,您必须在邮件编辑之前收集朋友的电子邮件地址。例如使用地址簿人员选择器控件。

无论你做什么,MFMail作曲家都不允许你在没有用户知识的情况下做任何事情。用户可以在弹出时删除/添加mailto字段。