看起来 Apple 在 UIActivityViewController 中错误地转换了字符串

Looks like Apple converts strings incorrectly in UIActivityViewController

我想弄清楚为什么 UIActivityViewController 发送一个稍微转换过的字符串来分享给邮件和微信。

这是我的代码:

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)

并且通过Mail分享时,显示为:

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

系统版本为10.3.3

×HTML4 entity,如果您将其作为纯文本共享,它会在电子邮件中将其自身转换为 × 符号.

将字符串转换为 URL

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)
}

通过在 mailWechat 中共享时给出特殊情况解决:

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


// 
extension String
{
     func xmlSimpleEscape() -> String
    {
        let mapList : SimpleToFromRepalceList = [
            ("&",  "&"),
            ("\"", """),
            ("'",  "'"),
            (">",  ">"),
            ("<",  "&lt;")]

        return self.simpleReplace(mapList: mapList)
    }
}

转义的一些变通方法html。我认为 Apple 应该在 UIActivityType 中提供建议的解析方法,并在接收字符串时进行自动转义。

您可以试试关注

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)

您也可以用它替换任何 url 特殊字符 HTML 名称。