Search code examples
iosswifturluiactivityviewcontroller

Looks like Apple converts strings incorrectly in UIActivityViewController


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.


Solution

  • 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.