English 中文(简体)
How to handle & in URL sent as HTML from iPhone Mail.app
原标题:

Apologies if this has been answered already. There are similar topics but none that I could find pertaining to Cocoa & NSStrings...

I m constructing a clickable URL to embed in an HTML email to be sent via the MFMailComposeViewController on the iPhone. i create the url then use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding to polish up white space, etc. then add some surrounding HTML to get:

<a href = "http://www.site.com/viewitem?name=Johnny%20Rotten&age=53&mate=sid%20vicious">view</a>

All s well so it s appended to emailBody. However once [mailComposer setMessageBody:emailBody isHTML:YES] all the & become &amp; which isn t ideal within my URL.

can i control this? is there a better encoding algorithm? my HTML is a bit rusty perhaps I m using the wrong encoding? I m sure on the server I could parse the &amp; back into & but looking for the Cocoa way...

Thanks!

最佳回答

Actually, & should always be encoded as &amp; in HTML attributes. Including links. Including form value delimiters. So it s done exactly what you want, even though you didn t know you wanted it.

Look at it this way: in your URL, you have &age=53... That s interpreted first as a character entity, and only after that doesn t work is it interpreted as an ampersand followed by more character data.

The W3C spec is quite clear on this:

Authors should use "&amp;" (ASCII decimal 38) instead of "&" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&amp;" in attribute values since character references are allowed within CDATA attribute values.

That should settle it: use &amp; not &.

问题回答

Are you calling MFMailComposeViewController s

setMessageBody:isHTML:

and what do you set isHTML to?

Depending on it s setting it might very well be that MFMailComposeViewController is trying to help you out be encoding the entire message body...

Either don t encode the body yourself or make the entire body HTML.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签