0

I try to add an anchor tag to local html file using this code:

let myAnchor ="#G"
let htmlPath = Bundle.main.path(forResource: "test", ofType: "html", inDirectory: "localWebsite", forLocalization:"")

let request = URLRequest(url: URL(fileURLWithPath: htmlPath! + myAnchor))
webview.load(request)

the answer is path/test.html%23G

so how I can convert to test.html#G ?

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
Thomas
  • 1
  • 1

2 Answers2

0

I don't know exactly what you want to achieve but you are speaking about encoding resp. decoding in your case. Your string is HTML encoded and you probably want to decode it. There are several answers for that already on this platform. One that would fit your needs is here: https://stackoverflow.com/a/39344394/1195661

palme
  • 2,499
  • 2
  • 21
  • 38
0

You can construct an anchor tag relative to the filepath URL like this:

let baseURL = URL(fileURLWithPath: htmlPath)
if let url = URL(string: myAnchor, relativeTo: baseURL) {
    print(url.absoluteURL)
    // do your things
}
Code Different
  • 90,614
  • 16
  • 144
  • 163