3

I was trying to figure out why UIActivityViewController sent a slightly converted string to share for Mail and WeChat.

This is my code:

let activityViewController = UIActivityViewController(activityItems: ["http://preprodgo.travelstart.com/search-on-index?version=3&timestamp=2017-09-15_10-31-27-031"], applicationActivities: nil)

self.present(activityViewController, animated: true, completion: nil)

And when shared by Mail, it was shown as:

http://preprodgo.travelstart.com/search-on-index?version=3×tamp=2017-09-15_10-31-27-031

The system version is 10.3.3.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
allenlinli
  • 2,066
  • 3
  • 27
  • 49

3 Answers3

8

× is a HTML4 entity and it will convert itself to a × symbol in an email if you share it as plain text.

Convert the string to a URL instead:

if let url = URL(string: "http://preprodgo.travelstart.com/search-on-index?version=3&timestamp=2017-09-15_10-31-27-031") {
    let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
    present(activityViewController, animated: true)
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • Thanks! And what I actually wanted to share is this: ``` message = String(format: NSLocalizedString("share_text_search_result_multi_city", comment: "Hey,\nI found great flights on Flapp from %1$@ from only %2$@ %3$d.\nCheck them out!\n\n%4$@"), String(describing: outboundAirportCityName), cheapestPriceCurrencyText, cheapestPrice, link) ``` Any idea for preventing convertion if I have to share it as plain text? – allenlinli Sep 15 '17 at 09:03
  • Include the strting and the URL as two separate activity items. That way, the URL will not be converted. – Tamás Sengel Sep 15 '17 at 09:06
2

Solved by giving special case when sharing in mail and Wechat:

@objc class TSActivityProvider: NSObject, UIActivityItemSource {
   func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
       //...
       if activityType == .mail || activityType == .postToWeibo {
            message = message.xmlSimpleEscape()
        }
       return message
   }
}


// https://stackoverflow.com/questions/803676/encode-nsstring-for-xml-html
extension String
{
     func xmlSimpleEscape() -> String
    {
        let mapList : SimpleToFromRepalceList = [
            ("&",  "&"),
            ("\"", """),
            ("'",  "'"),
            (">",  ">"),
            ("<",  "&lt;")]

        return self.simpleReplace(mapList: mapList)
    }
}

Kind of work around for escaping html. I think Apple should provide suggested parsing method in UIActivityType and do auto escaping when receiving in string.

allenlinli
  • 2,066
  • 3
  • 27
  • 49
0

You can try Following

vat urlString = "http://preprodgo.travelstart.com/search-on-index?version=3&timestamp=2017-09-15_10-31-27-031"
urlString = urlString.StringByReplacingOccurencOf(string:"&", with:"&amp")
let activityViewController = UIActivityViewController(activityItems: [urlString], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)

You can can also replace any url Special character with it HTML Name.

Abu Ul Hassan
  • 1,340
  • 11
  • 28