MFMailComposeViewController:如何将可点击的URL链接嵌入电子邮件消息正文?

14

我希望我的应用程序使用MFMailComposeViewController发送电子邮件,以便收件人可以单击嵌入的url打开对应的网站。 MFMailComposeViewController似乎不明确支持此功能。 有什么想法吗?

5个回答

27

我删除了之前的答案,因为它是不正确和无关的。经过长时间的纠结,最终我弄清楚了在我的情况下发生了什么,并且很可能也是这个问题中发生的情况。

当你为MFMailComposeViewController编写HTML正文时,必须在HTML中加入换行符。如果任何一行长度>76个字符,那么正文将被解释如下:

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

如果您添加换行符,则会取消 Content-Transfer-Encoding: quoted-printable,并且一切都会如预期般工作。假设您的HTML格式正确。

例如,可以按以下方式构建正文:

NSMutableString *body = [NSMutableString string];
// add HTML before the link here with line breaks (\n)
[body appendString:@"<h1>Hello User!</h1>\n"];
[body appendString:@"<a href=\"http://www.mysite.com/path/to/link\">Click Me!</a>\n"];
[body appendString:@"<div>Thanks much!</div>\n"];

祝福!


1
太好了!知道换行是这样的,省了我不少时间,谢谢! - manuelBetancurt

18

是的,你可以这样做:

MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:subject];
[composer setMessageBody:message isHTML:YES];                    

message是一个包含HTML内容的NSString。您可以在其中添加任何HTML。


9

我有同样的问题。

我的链接是HTML,我可以看到“蓝色”,但如果我点击它,Safari移动版不会打开。我被允许编辑文本。

在一个类中,我有这个:

-(id) init{
    self = [super init];
    if (self) {
            if ([MFMailComposeViewController canSendMail]) {
                self.mailComposeDelegate = self;
                [self setSubject: @"Subject"];
                [self setMessageBody: @"<h2>Body</h2><a href='http://www.google.com'>link example</a>" isHTML: YES];
            }
            else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Mail Accounts" 
                                                                message:@"You don't have a Mail account configured, please configure to send email."
                                                               delegate:nil 
                                                      cancelButtonTitle:@"OK" 
                                                      otherButtonTitles: nil];
                [alert show];
            }
    }
    return self;
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [controller dismissModalViewControllerAnimated: YES];
}

这里是iPad的截图: iPad Screen shot 如果我发送邮件后,再进入“已发送”邮箱,链接就能正常工作,所以我认为问题出在打开链接的事件上。
谢谢。

你点击完成了吗?如果是的话,请在这里发布链接重定向@Oceanicsix,谢谢。 - Maul
嗨,这已经是一年多以前的事了,如果我现在诚实地说,我已经不记得我是如何解决这个问题的了。抱歉并祝好运! - Mikel

6
使用setMessageBody:isHTML:方法,在正文中传递一个正确的HTML链接(<a href="你的链接">你的链接文字</a>),并将isHTML参数设置为YES

5

你试过在你的代码中尝试你的建议了吗?在来到这个网站之前,我已经尝试过了,但抱歉地说,它根本不起作用。链接的颜色确实是蓝色的,HTML 也被解析了,但没有链接可用。当我点击链接时,我只能编辑它......

有更好的建议吗?


你是在收到的邮件上点击还是在编辑模式下点击的? - AsifHabib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接