简体   繁体   中英

How to fill email with To and Subject in iPhone?

I have a button in my app called contact Us! Is there a way to open up the Eamil client in iPhone filled with To and Subject that I provide?

You will want to use the MFMailComposeViewController class. Here's the relevant part from Apple's 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];

The MailComposer sample also shows you how to open the external mail app:

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

Check out MFMailComposeViewController . You can set the Subject, To Field, and even populate the body with HTML.

Sure you can.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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