URL from IPv6 address string returning nil object

I have IPv6 address string with an interface name. like fc00::1%utun3.

when I am trying to create URL object with this ip address string it is returning nil

let url  = URL(string: "[fc00::1]%utun3")
print("Output -> \(url))
-----------
Output -> nil

is it any way that we can create an url with interface/utun name?

Thanks-:

Answered by DTS Engineer in 752441022

You can’t just create a URL from a IP address. You need a scheme. So:

URL(string: "ws://[fc00::1]")

If you want to include the scope, you need to percent encode the %:

URL(string: "ws://[fc00::1%25utun3]")

For stuff like this, URLComponents is your friend.

As to whether that’ll actually work for networking… sheesh… I’ve no idea.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

You can’t just create a URL from a IP address. You need a scheme. So:

URL(string: "ws://[fc00::1]")

If you want to include the scope, you need to percent encode the %:

URL(string: "ws://[fc00::1%25utun3]")

For stuff like this, URLComponents is your friend.

As to whether that’ll actually work for networking… sheesh… I’ve no idea.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

URL from IPv6 address string returning nil object
 
 
Q