如何转义href = mailto(iPhone)中的字符

时间:2009-03-08 19:31:22

标签: objective-c iphone cocoa-touch uiwebview

我想在UIWebView上创建一个链接,用于将内容通过电子邮件发送给用户。一个简单的例子是:

<a href="mailto:zippy@example.com?subject=Sarcasm&body=I »
<b>love</b> &lt;html&gt; mail!">Hi!</a>

这会创建一条如下所示的消息:

- 开始消息---

收件人:zippy@example.com 主题:讽刺

我喜欢邮件!

- 结束消息 -

我需要更精细的东西。主题将包含多个带空格的单词。正文将包含HTML,列表(&lt; ul&gt;)和在其href中带引号的超链接。我该如何创造这样的东西?

以下是一个例子:

subject =“这只是一个测试”

body =“这是身体的一部分。这是一个链接列表:
&LT; UL&GT;
  &lt; li&gt;&lt; a href =“http://www.abc.com”&gt; abc.com&lt; / a&gt;&lt; / li&gt;

  &lt; li&gt;&lt; a href =“http://www.xyz.com”&gt; xyz.com&lt; / a&gt;&lt; / li&gt;

&LT; / UL&GT;
结束。“

另外,为什么模拟器在点击mailto链接时会做任何事情?

3 个答案:

答案 0 :(得分:3)

字段是URL编码的(在Cocoa中,您可以使用stringByAddingPercentEscapesUsingEncoding:)。

4thspace提到Mail.app does allow HTML。这是反对的 mailto RFC2368,明确指出body应该是text/plain

答案 1 :(得分:2)

模拟器没有Mail.app,正如您可以在主屏幕上看到的那样,因此在遇到mailto链接时无法打开。

据我所知,没有办法使用mailto:发送html格式的电子邮件。

答案 2 :(得分:1)

您需要对项目的MessageUI.framework引用。

将以下内容添加到.h文件

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

添加代理<MFMailComposeViewControllerDelegate>

在.m文件中创建几个与以下类似的方法。

-(IBAction)checkCanSendMail:(id)sender{
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil) {
            if ([mailClass canSendMail]) {
                [self displayComposerSheet];
            }
            else {
                //Display alert for not compatible. Need iPhone OS 3.0 or greater. Or implement alternative method of sending email.
            }
        }
        else {
            //Display alert for not compatible. Need iPhone OS 3.0 or greater.  Or implement alternative method of sending email.
        }
    }

    -(void)displayComposerSheet {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Email Subject"];

        //Set our to address, cc and bcc
        NSArray *toRecipients = [NSArray arrayWithObject:@"primary@domain.com"];
        //NSArray *ccRecipients = [NSArray arrayWithObjects:@"first@domain.com",@"second@domain.com",nil];
        //NSArray *bccRecipients = [NSArray arrayWithObjects:@"first@domain.com",@"second@domain.com",nil];

        [mailer setToRecipients:toRecipients];
        //[mailer setCcRecipients:ccRecipients];
        //[mailer setBccRecipients:bccRecipients];

        NSString *emailBody = @"\
        <html><head>\
        </head><body>\
        This is some HTML text\
        </body></html>";

        [mailer setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:mailer animated:YES];
        [mailer release];
        }

Apple示例代码,其中包含更多说明:http://developer.apple.com/iphone/library/samplecode/MailComposer/

我知道这不会使用webView,但它确实允许您在应用程序中创建HTML电子邮件。