user1876657
user1876657

Reputation: 71

Webview loadRequest not working

In my application, i'm using a Webview to load different versions of a website based on user's preferred language.Using WebFrame's loadRequest method for this -

[[aWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aStr]]];

If, aStr contains english alphabets only,i.e if aStr = http : // ....../language/English ,then its working fine & the webpage loads. But if aStr is something like http: //....../language/ウンウンウン(Japanese), nothing happens, neither WebView loads, nor it throws any error. If i paste the same link in Safari, the webpage loads there. Any suggestion on how to fix this issue?

I've also tried [aWebView setMainFrameURL:aStr] method. Same issue there too.

Upvotes: 3

Views: 2473

Answers (1)

Fruity Geek
Fruity Geek

Reputation: 7381

You have to escape the invalid URL characters in aStr before you can use it to create the NSURL using [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding].

//Encode invalid URL characters
aStr = [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
[[aWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aStr]]];

URLWithString: returns a nil value if the string is invalid. Which is why you your UIWebView has no error and does not load anything.

Upvotes: 3

Related Questions