从 iPhone 应用程序内通过电子邮件发送 HTML 在特殊字符处停止

发布于 2024-08-29 18:26:05 字数 1092 浏览 6 评论 0原文

我有一个 iPhone 应用程序,可以让用户通过电子邮件将一些预先确定的文本作为 HTML 发送。

我遇到的问题是,如果文本中包含特殊字符(例如,与号 &、>、<),我用于发送电子邮件正文的 NSString 变量会在特殊字符处被截断。

我不知道如何解决这个问题(我尝试使用方法 stringByAddingPercentEscapesUsingEncoding...但这并没有解决问题)。

关于我做错了什么/如何解决它的想法?

这是示例代码,显示我正在尝试做什么

谢谢!

- (void)send_an_email:(id)sender {
    NSString *subject_string    = [NSString stringWithFormat:@"Summary of %@", commercial_name];
    NSString *body_string       = [NSString stringWithFormat:@"%@<br /><br />", [self.dl email_message]]; // email_message returns the body of text that should be shipped as html.  If email_message contains special characters, the text truncates at the special character
    NSString *full_string       = [NSString stringWithFormat:@"mailto:?to=&subject=%@&body=%@", [subject_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [body_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:full_string]];
}

I have an iPhone app that will let users email some pre-determined text as HTML.

I'm having a problem in that if the text contains special characters within the text (e.g., ampersand &, >, <), the NSString variable that I use for sending the body of the email gets truncated at the special character.

I'm not sure how to fix this (I tried using the method stringByAddingPercentEscapesUsingEncoding…but this hasn't fixed the problems).

Thoughts on what I'm doing wrong / how to fix it?

Here is sample code showing what I'm trying to do

Thanks!!!

- (void)send_an_email:(id)sender {
    NSString *subject_string    = [NSString stringWithFormat:@"Summary of %@", commercial_name];
    NSString *body_string       = [NSString stringWithFormat:@"%@<br /><br />", [self.dl email_message]]; // email_message returns the body of text that should be shipped as html.  If email_message contains special characters, the text truncates at the special character
    NSString *full_string       = [NSString stringWithFormat:@"mailto:?to=&subject=%@&body=%@", [subject_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [body_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:full_string]];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-09-05 18:26:05

这适用于我从应用程序内发送电子邮件。按照您的方式,它会退出应用程序并打开邮件。尝试这样的事情:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
... do your email setup code
[picker setMessageBody:emailBody isHTML:YES];

这里是一个很好的教程

This works for me to send email from within the app. The way you have it, it quits the app and opens Mail. Try something like this:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
... do your email setup code
[picker setMessageBody:emailBody isHTML:YES];

Here is a good tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文