iosswifturlnsurlcomponents

Swift 4 How to remove %EF%BB%BF from URL created from URLComponent()?


I attempt to create URL from components

var com =  URLComponents()
com.scheme = "http"
com.host = "192.168.1.99"
com.port = 77
com.path = "/api/dosomething/55"

print("SHOW ME URl =  \(com.url!))")

What I got is something like this

http://192.168.1.99:77/%EF%BB%BFapi/dosomething/55

Always got %EF%BB%BF, in which case the url becomes invalid

I want to remove %EF%BB%BFfrom the URL

How can I do that?


Solution

  • That's a URL-encoding of a UTF-8 BOM, it may be caused by processing of the URLs with some text editors. Trim white spaces from your path string:

    var com =  URLComponents()
    com.scheme = "http"
    com.host = "192.168.1.99"
    com.port = 77
    let path = //The path
    let trimmedPath = path.trimmingCharacters(in: .whitespaces)
    com.path = trimmedPath
    
    print("SHOW ME URl =  \(com.url!)")
    

    In utf-8, the 'ZERO WIDTH NO-BREAK SPACE' is encoded as 0xEF 0xBB 0xBF