Can WKWebView support encoded url?

176 views Asked by At

If I use an encoded url to open in WKWebView, this webView can not open this link。

NSString* request = @"http%3A%2F%2Fwww.baidu.com%0A";
NSURL* url = [NSURL URLWithString:request];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];

So I must to decode the url before passed it to WKWebView . Any other pretty way to make the WKWebView support encoded url?

1

There are 1 answers

0
dgatwood On BEST ANSWER

No, there's no other way. Decode the URL. I'm guessing you got this from a URL query string field. If so, take advantage of NSURLComponents. That makes it easy to grab the unencoded value for a query string part.

NSString *valueForKeyInURL(NSString *key, NSURL *URL) {
    NSURLComponents *components =
        [NSURLComponents componentsWithURL:URL
                   resolvingAgainstBaseURL:NO];
    NSURLQueryItem *theField = nil;
    for (NSURLQueryItem *item in components.queryItems) {
        if ([item.name isEqual:key]) {
            theField = item;
            break;
        }
    }
    return item.value;
}