如何在xcode中为您的应用实施电子邮件?

时间:2011-05-14 18:25:22

标签: iphone xcode email

我尝试将'mail composer'(来自dev的示例代码)实现到我的应用程序。它们在示例代码中有很多硬编码。例如:发送给收件人 - first@example.com。我希望将其留空@"",但它会自动在电子邮件地址前面加逗号。我还有其他问题,基于apple DEV readme.txt,如果launchMailAppOnDevice失败,将会触发displayComposerSheet。我应该更换launchMailAppOnDevice中的所有硬编码以及如何操作吗? 请指教。

-(IBAction)showPicker:(id)sender
{
        // This sample can run on devices running iPhone OS 2.0 or later  
        // The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
        // So, we must verify the existence of the above class and provide a workaround for devices running 
        // earlier versions of the iPhone OS. 
        // We display an email composition interface if MFMailComposeViewController exists and the device can send emails.
        // We launch the Mail application on the device, otherwise.

        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil)
        {
                // We must always check whether the current device is configured for sending emails
                if ([mailClass canSendMail])
                {
                        [self displayComposerSheet];
                }
                else
                {
                        [self launchMailAppOnDevice];
                }
        }
        else
        {
                [self launchMailAppOnDevice];
        }
}


// Displays an email composition interface inside the application. Populates all the Mail fields. 

-(void)displayComposerSheet 

{
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

        [picker setSubject:@"Visit my new apps in app store!"];


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


        [picker setToRecipients:toRecipients];

        //Replace the email body with the content from my textview

        [picker setMessageBody:TextView.text isHTML:NO];

        [self presentModalViewController:picker animated:YES];

    [picker release];
}


// Launches the Mail application on the device.

-(void)launchMailAppOnDevice
{
        NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@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]];
}

2 个答案:

答案 0 :(得分:0)

您是否在.h文件中实现了邮件类

答案 1 :(得分:0)

替换您的硬编码值 “launchMailAppOnDevice”..它适用于我的