我试图找出为什么UIActivityViewController
发送了一个稍微转换后的字符串来分享Mail和微信。
这是我的代码:
let activityViewController = UIActivityViewController(activityItems: ["http://preprodgo.travelstart.com/search-on-index?version=3×tamp=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。
答案 0 :(得分:8)
×
是 HTML4 entity ,如果您将其作为纯文本分享,它会将自身转换为电子邮件中的×
符号。
将字符串转换为URL
代替:
if let url = URL(string: "http://preprodgo.travelstart.com/search-on-index?version=3×tamp=2017-09-15_10-31-27-031") {
let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(activityViewController, animated: true)
}
答案 1 :(得分:2)
在mail
和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 = [
("&", "&"),
("\"", """),
("'", "'"),
(">", ">"),
("<", "<")]
return self.simpleReplace(mapList: mapList)
}
}
有关逃避HTML的工作。我认为Apple应该在UIActivityType
中提供建议的解析方法,并在收到字符串时自动转义。
答案 2 :(得分:0)
您可以尝试以下
vat urlString = "http://preprodgo.travelstart.com/search-on-index?version=3×tamp=2017-09-15_10-31-27-031"
urlString = urlString.StringByReplacingOccurencOf(string:"&", with:"&")
let activityViewController = UIActivityViewController(activityItems: [urlString], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
你也可以用HTML名称替换任何url特殊字符。