Sample code for 30+ languages & platforms
Swift

Parse a URL into its Component Parts

See more HTTP Examples

Demonstrates how to parse a URL into it's component parts.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let url = CkoUrl()!

    var urlStr: String? = "https://www.amazon.com/Anarchy-State-Utopia-Robert-Nozick/dp/0465051006/ref=sr_1_1?s=books&ie=UTF8&qid=1430344305&sr=1-1&keywords=nozick#frag123"

    success = url.parse(url: urlStr)
    // Assume success..

    print("URL: \(urlStr!)")
    print("Host: \(url.host!)")
    print("Port: \(url.port.intValue)")
    print("HostType: \(url.hostType!)")
    print("Ssl: \(url.ssl)")
    print("Path: \(url.path!)")
    print("Query: \(url.query!)")
    print("Frag: \(url.frag!)")
    print("----")

    urlStr = "http://matt:secret@www.chilkatsoft.com:8080/somepath.asp?test=123&size=2"

    success = url.parse(url: urlStr)
    // Assume success..

    print("URL: \(urlStr!)")
    print("Host: \(url.host!)")
    print("Port: \(url.port.intValue)")
    print("HostType: \(url.hostType!)")
    print("Ssl: \(url.ssl)")
    print("Login: \(url.login!)")
    print("Password: \(url.password!)")
    print("Path: \(url.path!)")
    print("Query: \(url.query!)")
    print("Frag: \(url.frag!)")

}