swift - 特殊字符在 URLQueryItems 中没有被正确编码

标签 swift string special-characters deep-linking firebase-dynamic-links

我需要在我的应用程序中创建 firebase 深层链接,以便使用 UIActivityViewController 在不同平台上共享它们。 为了创建这些链接,我使用以下代码和 URLQueryItem 来获取我将分享给其他应用程序的 url:

func createURLWithComponents(EventToUrl: Event) -> URL? {

    var urlComponents = URLComponents()
    urlComponents.scheme = "https"
    urlComponents.host = "www.example.com"
    urlComponents.path = "/"

    // add params
    let id = URLQueryItem(name: "id", value: EventToUrl.eventID)

    print(EventToUrl.name)
    let name = URLQueryItem(name: "name", value: EventToUrl.name)
    print(name.description)
    print(name.value)
    let photo = URLQueryItem(name: "photo", value:  EventToUrl.photoUrl)
    let created = URLQueryItem(name: "created", value: EventToUrl.creationDate )
    let participants = URLQueryItem(name: "participants", value: String(EventToUrl.Participants.count) )
    let place = URLQueryItem(name: "place", value: EventToUrl.Places[0].name)
    let time = URLQueryItem(name: "time", value: EventToUrl.time[0].TimestampString)
    urlComponents.queryItems = [id, name, photo, created, participants , place , time]

    let dynamicLinkDomain : String = "***.app.goo.gl"
    var deepLink = URLComponents()
    deepLink.scheme = "https"
    deepLink.host = dynamicLinkDomain
    deepLink.path = "/"

    let link = URLQueryItem(name: "link", value:  urlComponents.url?.absoluteString)
    let apn = URLQueryItem(name: "apn", value: "***" )
    let ibi = URLQueryItem(name: "ibi", value: "***" )
    let d = URLQueryItem(name: "d", value: "1")
    deepLink.queryItems = [link, apn, ibi, d]
    print(deepLink.url?.absoluteString)
    print(deepLink.url)

    return deepLink.url
}

这是我发给他的链接:

https://***.app.goo.gl/?link=https://www.example.com/?id%3D-KZYfNRCXdSqUG72FmEQ%26name%3Djeremie's%2520Event%26photo%3D%26created%3D%26participants%3D1%26place%3DLa%2520Piazza%26time%3D1482362284.50304&apn=***&ibi=***&d=1

https://.app.goo.gl/?link= https://www.example.com/?id%3D-KZYfNRCXdSqUG72FmEQ%26name%3Djeremie 's%2520Event%26photo%3D%26created%3D%26participants%3D1%26place%3DLa%2520Piazza%26time%3D1482362284.50304&apn=&ibi=***&d=1

如您所见,'如果事件名称由“'”字符组成(Jeremie 的事件),URLQueryItem 不会正确编码此字符(在我的 android 应用程序中,系统函数将其转换为“'”--> %27) 我如何对其进行编码? PS:我尝试使用 addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) 没有成功...... 感谢您的帮助!

最佳答案

我找到了一个比其他解决方案更简单的解决方案。 (至少对我来说是这样)

我像往常一样构造我的 URLComponents,但在从中获取 url 之前,我获取了 urlComponents.string 并将特殊代码替换为符号。我通过 addingPercentEncoding 运行该字符串,然后从该字符串创建一个 url。

let stringWithoutBS = urlComponents.string!.replacingOccurrences(of: "%27", with: "'")
        //ignore the force unwrap

guard let encodedURL = stringWithoutBS.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {
            print("unhelpful message")
            return nil
        }

var correctURL = URL(string: encodedURL)

关于swift - 特殊字符在 URLQueryItems 中没有被正确编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41273994/

相关文章:

swift - 添加 NSLayoutConstraint 后, View 消失

python:将列表转换为字符串,仅当 ','不存在时才添加 '|'

c - 在c中输入字符串以及如何将字符串存储到变量中

mysql - 是否有任何范围可以转义 mysql 查询中的所有特殊字符?

ios - 如何在同一个 View Controller 中使用多个搜索 Controller ?

arrays - 如何在 swift 中为 UIPickerview 添加一个新元素到一个 2 组件数组

ios - 如何使用 Swift 从其父级的 Nib 创建 View

c++ - 如何让 STL std::string 在 Windows 上使用 unicode?

batch-file - 如何在选择命令中使用特殊字符 - 批处理文件

php - 如何将特殊字符插入数据库?