Randy Randy - 4 months ago 10
iOS Question

Can't do google search with "&" character on WKWebView

I have a

WKWebView
in my app that allows a user to search on Google.

Everything works fine but searches with "&" character. For instance, if I search for at&t, I will have the results of the search of at.

If I print the url of my web view in
didCommitNavigation
, I can see the right searched terms at&t

func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!)
{
print(webView.URL?.absoluteString)
}


returns


https://www.google.fr/search?hl=en&q=at&t=&gws_rd=cr,ssl&ei=8f-hV4iJGM-ba6qvh-gJ


We can see the q parameter :
q=at&t
whereas the result page is the same as the one for
q=at
.

Any ideas to solve that problem would be appreciated.

Answer

As this verifies, the & is not automatically escaped in the usual way, so you probably have to process your search string manually (wherever your user enters the terms, just replace the & when you get it from there):

let searchterm = getTermFromWherever()
let actualterm = searchterm.stringByReplacingOccurrencesOfString("&", withString: "%26")

This should work.