Webview loadRequest not working

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

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

If, aStr contains only English alphabets, i.e. if aStr = http: //....../language/English then its working tone and web page are loaded. But if aStr is something like http: //....../language/ ウ ン ウ ン ウ ン (Japanese), nothing happens, neither loading the WebView nor an error. If I paste the same link in Safari, the web page is loaded there. Any suggestion on how to fix this issue?

I also tried the [aWebView setMainFrameURL: aStr] method. There is also a problem.

+3


source to share


1 answer


You need to avoid invalid URL characters aStr

before you can use it to create NSURLs with [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

.

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

      



URLWithString:

returns nil if the string is invalid. This is why your UIWebView has no error and nothing is loading.

+3


source







All Articles