如何快速将url作为url参数传递?

时间:2019-07-16 15:46:36

标签: ios swift

我必须将url作为另一个URL的参数传递。例如

https://www.host1.com?returnURL=https://www.host2.com/path?queryparamater1=value&queryparameter2=value


var returnUrlComponents = URLComponents()
        returnUrlComponents.scheme = "https"
        returnUrlComponents.host = "www.host.com"
        returnUrlComponents.path = "/path1"
        let queryItem1 = URLQueryItem(name: "param1", value: "value1")
        let queryItem2 = URLQueryItem(name: "redirect_uri", value: "test://result")
        returnUrlComponents.queryItems = [queryItem1, queryItem2]
        var urlComponents = URLComponents()

        urlComponents.scheme = "https"
        urlComponents.host = "www.anotherhost.com"
        urlComponents.path = "/path2"
        urlComponents.queryItems = [URLQueryItem(name: "returnURL", value: returnUrlComponents.url?.absoluteString)]
        print(urlComponents.url?.absoluteString)

输出是这个-https://www.anotherhost.com/path2?returnURL=https://www.host.com/path1?param1%3Dvalue1%26redirect_uri%3Dtest://result

预期的输出-https://www.anotherhost.com/path2?returnURL=https%3A%2F%2Fwww.host.com%2Fpath1%3Fparam1%3Dvalue1%26redirect_uri%3Dtest%3A%2F%2Fresult

2 个答案:

答案 0 :(得分:1)

如果您正在寻找一个特定的URL模式,则可能必须自己构建字符串。如果您可以使用URL编码参数,则可以将addingPercentEncodingurlHostAllowed一起使用。

var returnUrlComponents = URLComponents()
returnUrlComponents.scheme = "https"
returnUrlComponents.host = "www.host.com"
returnUrlComponents.path = "/path1"
let queryItem1 = URLQueryItem(name: "param1", value: "value1")
let queryItem2 = URLQueryItem(name: "redirect_uri", value: "test://result")
returnUrlComponents.queryItems = [queryItem1, queryItem2]

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

let encodedReturnUrlComponents = returnUrlComponents.url?.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
urlComponents.queryItems = [URLQueryItem(name: "returnURL", value: encodedReturnUrlComponents)]

print(urlComponents.url?.absoluteString)

该输出为https://www.anotherhost.com/path2?returnURL=https%253A%252F%252Fwww.host.com%252Fpath1%253Fparam1%3Dvalue1%26redirect_uri%3Dtest%253A%252F%252Fresult

不过请注意,由于它是查询参数,因此编码将被双重编码。例如,将使用%253A代替%3A(最后代表“:”)。

答案 1 :(得分:0)

我通过构建url本身获得了所需的输出。不是干净的好代码,但它可以工作。

var returnUrlComponents = URLComponents()
        returnUrlComponents.scheme = "https"
        returnUrlComponents.host = "www.host.com"
        returnUrlComponents.path = "/path1"
        let queryItem1 = URLQueryItem(name: "param1", value: "value1")
        let queryItem2 = URLQueryItem(name: "returnURI", value: "test://result")
        returnUrlComponents.queryItems = [queryItem1, queryItem2]
        var urlComponents = URLComponents()

        urlComponents.scheme = "https"
        urlComponents.host = "www.anotherhost.com"
        urlComponents.path = "/path2"

        let unreservedCharset = CharacterSet(charactersIn: "-._~" )
        var allowedCharacterSet = CharacterSet.alphanumerics
        allowedCharacterSet.formUnion(unreservedCharset)

        var urlString = urlComponents.url?.absoluteString
        urlString?.append("?returnURL=")
        if let encodedURLString = returnUrlComponents.url?.absoluteString.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) {
            urlString?.append(encodedURLString)
        }
//This step is not necessary. urlString can be printed directly.
        if let urlStr = urlString, let urlComp = URLComponents(string: urlStr) {
            urlComponents = urlComp
        }
        print(urlComponents.url?.absoluteString)