【问题标题】:UTF8 strings in Javascript window.open to be intercepted by UIWebview's shouldStartLoadWithRequestJavascript window.open 中的 UTF8 字符串被 UIWebview 的 shouldStartLoadWithRequest 拦截
【发布时间】:2012-08-26 15:15:59
【问题描述】:

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

JS代码:

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

Obj C 代码:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

   NSLog([[request URL] absoluteString]);
   return YES;

}

控制台输出:

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

【问题讨论】:

    标签: javascript objective-c utf-8 uiwebview


    【解决方案1】:

    您将字符串用作NSLog()格式。与

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

    你会得到预期的输出。

    详解:ǐ的UTF-8序列是C7 90。shouldStartLoadWithRequest[[request URL] absoluteString]的内容是

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

    如果你使用它作为格式字符串,“%C”将被一个随机字符替换。

    要摆脱百分比转义,请使用

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

    【讨论】:

    • 非常感谢。它仍然无法正常工作......我现在得到 nothing.com/?ST=n%C7%90 。所以现在的问题是,我如何用它的 UTF8 hexcode %C7%90 组成一个 NSString
    • @Amarsh:我已将其添加到我的答案中。
    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多