Javascript窗口.open中的UTF8字符串将被UIWebview's应该启动LoadWithReque

UTF8 strings in Javascript window.open to be intercepted by UIWebview's shouldStartLoadWithRequest

本文关键字:LoadWithReque 启动 UIWebview 中的 open 窗口 UTF8 字符串 Javascript      更新时间:2023-09-26

我的UIWebview中有一个带有JS的页面,它将向我的Obj C代码发送一些非英语文本。当我对我在Obj C代码中收到的内容进行NSlog时,我会得到混乱的输出。有人能看到这里出了什么问题吗:

JS代码:

window.open("http://nothing.com?ST=nǐ",null);

目标C代码:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
   NSLog([[request URL] absoluteString]);
   return YES;
}

控制台输出:

http://nothing.com?ST=n㽲79

您使用字符串作为NSLog()格式。带

NSLog(@"%@", [[request URL] absoluteString]);

您将获得预期的输出。

详细的解释:在UTF-8的序列中,音为C790。shouldStartLoadWithRequest[[request URL] absoluteString]的内容为

http://nothing.com/?ST=n%C7%90

如果将其用作格式字符串,则"%C"将被一个随机字符取代。

要消除百分比逃逸,请使用

NSString *url = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];