【问题标题】:How to implement email for your apps in xcode?如何在 xcode 中为您的应用程序实现电子邮件?
【发布时间】:2011-05-14 18:25:22
【问题描述】:

我尝试在我的应用中实现“邮件编写器”(来自开发的示例代码)。它们在示例代码中有很多硬编码。例如:发送给收件人 - first@example.com。我想像@"" 一样将其留空,但是它会自动在电子邮件地址前面添加逗号前缀。我还有其他顾虑,基于apple DEV readme.txt,如果displayComposerSheet 失败,将触发launchMailAppOnDevice。我应该替换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]];
}

【问题讨论】:

    标签: iphone xcode email


    【解决方案1】:

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

    【讨论】:

    • 是的。我只想知道如何将 toRecipients 变量留空? NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];如果我这样做,当用户输入电子邮件地址时,它将在电子邮件地址前加上逗号 (,)。 NSArray *toRecipients = [NSArray arrayWithObject:@""];
    • 我在对象之后添加了“s”,它可以工作。 NSArray *toRecipients = [NSArray arrayWithObjects: nil];
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2010-12-20
      • 2010-09-11
      • 2018-03-10
      相关资源
      最近更新 更多