Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift URLQueryItems中未正确编码的特殊字符_Swift_String_Special Characters_Deep Linking_Firebase Dynamic Links - Fatal编程技术网

Swift URLQueryItems中未正确编码的特殊字符

Swift URLQueryItems中未正确编码的特殊字符,swift,string,special-characters,deep-linking,firebase-dynamic-links,Swift,String,Special Characters,Deep Linking,Firebase Dynamic Links,我需要在我的应用程序中创建firebase深度链接,以便使用UIActivityViewController在不同平台上共享这些链接。 为了创建这些链接,我使用URLQueryItem的以下代码获取我将共享给其他应用程序的url: func createURLWithComponents(EventToUrl: Event) -> URL? { var urlComponents = URLComponents() urlComponents.scheme = "http

我需要在我的应用程序中创建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='s%2520事件%26照片%3D%26创建%3D%26参与者%3D1%26地点%3DLa%2520广场%26时间%3D1482362284.50304&apn=&ibi=***&d=1

正如您可以看到的“如果事件名称由“””字符组成(Jeremie的事件),URLQueryItem没有正确编码此字符(在我的android应用程序中,系统函数将其转换为“”-->%27) 我如何对它进行编码? PS:我尝试使用
添加百分比编码(允许使用字符:.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)

是的,这确实令人沮丧,因为它也没有正确地编码
+
字符。您可以选择
.urlQueryAllowed
并删除几个字符,也可以自己手动构建。有关Swift 2或Swift 3,请参见。@Rob如果我想使用您的解决方案(第一个带有扩展名的解决方案),我是否要在func URLQueryParameterAllowedCharacterSet()中添加“'”字符?不,您不想添加它。如果一组字符是允许的,您希望将其省略,以便它将被转义百分比。。