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

时间:2017-09-15 08:50:45

标签: ios swift url uiactivityviewcontroller

我试图找出为什么UIActivityViewController发送了一个稍微转换后的字符串来分享Mail和微信。

这是我的代码:

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。

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

mailWechat分享时给出特殊情况解决:

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

有关逃避HTML的工作。我认为Apple应该在UIActivityType中提供建议的解析方法,并在收到字符串时自动转义。

答案 2 :(得分:0)

您可以尝试以下

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)

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