如何用iPhone中的To和Subject填写电子邮件?

时间:2011-10-27 18:37:04

标签: objective-c ios

我的应用程序中有一个按钮,请联系我们!有没有办法在iPhone中打开Eamil客户端,其中包含我提供的To和Subject?

3 个答案:

答案 0 :(得分:1)

您需要使用MFMailComposeViewController课程。以下是Apple MailComposer example的相关部分:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];

// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
[picker setToRecipients:toRecipients];

[self presentModalViewController:picker animated:YES];
[picker release];

MailComposer示例还向您展示了如何打开外部邮件应用程序:

NSString *recipients = @"mailto:first@example.com&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

答案 1 :(得分:0)

结帐MFMailComposeViewController。您可以设置Subject,To Field,甚至可以使用HTML填充正文。

答案 2 :(得分:0)

当然可以。

- (void)emailExport:(NSString *)filePath
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    // Set the subject of email
    [picker setSubject:@"My desired subject"];

    // Add email addresses
    // Notice three sections: "to" "cc" and "bcc"   

    NSString *valueForEmail = @"myEmail@gmail.com";
    NSString *valueForCCEmail = @"myCcEmail";
    if( valueForEmail == nil ||  [valueForEmail isEqualToString:@""])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Please set an email address before sending a time entry!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];        

        return;
    }
    else {
        [picker setToRecipients:[NSArray arrayWithObjects:valueForEmail, nil]];
    }

    if(valueForCCEmail != nil || ![valueForCCEmail isEqualToString:@""])
    {
        [picker setCcRecipients:[NSArray arrayWithObjects:valueForCCEmail, nil]];
    }

    // Fill out the email body text
    NSString *emailBody = @"My email body text.";

    // This is not an HTML formatted email
    [picker setMessageBody:emailBody isHTML:NO];

    // Show email view  
    [self presentModalViewController:picker animated:YES];

    // Release picker
    [picker release];
}