【问题标题】:How to escape characters in href=mailto (iPhone)如何在 href=mailto (iPhone) 中转义字符
【发布时间】:2009-03-08 19:31:21
【问题描述】:

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

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

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

-- 开始消息 ---

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

我喜欢邮件!

-- 结束信息--

我需要更详细的东西。主题将包含多个带有空格的单词。正文将包含 HTML、列表 (

    ) 和在其 href 中带有引号的超链接。我如何创建这样的东西?

    这是一个例子:

    subject="这只是一个测试"

    body="这是正文部分。这是链接列表:


    结束。”

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

【问题讨论】:

    标签: objective-c iphone cocoa-touch uiwebview


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      模拟器没有Mail.app,从它的主屏幕可以看到,所以当它遇到mailto链接时它没有任何东西可以打开。

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

      【讨论】:

      【解决方案3】:

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

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

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

      添加代理&lt;MFMailComposeViewControllerDelegate&gt;

      在您的 .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 电子邮件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-03
        • 2023-03-07
        • 2018-12-20
        • 1970-01-01
        • 2010-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多