开发者

Javascript Hex Escaped String returned to Cocoa App without Escaped Characters

Part of an iPhone appli开发者_Python百科cation that I'm developing is using a UIWebView to get the onclick event for certain elements on a page. One issue which has cropped up is on certain pages, like the iPhone version of http://news.google.com, the page has already hex escaped the links like in the following:

javascript:window.open('/news/url?sa\x3dt\x26ct3\x3dMAA4CEgAUABgAWoCdXN6AWjYAQE\x26usg\x3dAFQjCNGUV1AFw0LsmkcQzNnvo69ma_dhhA\x26rt\x3dHOMEPAGE\x26url\x3dhttp://www.cnn.com/2010/US/11/30/wikileaks/?hpt%3DSbin');void(0);  

When I receive the function from the UIWebView using the following method:

[webView stringByEvaluatingJavaScriptFromString:
                     [NSString stringWithFormat:@"SomeFunction(%i,%i);",(NSInteger)pt.x,(NSInteger)pt.y]]  

I receive back a String with the escaped characters in it as actual characters. So I receive back from the above method the javascript call with the hex escapes instead of:

javascript:window.open('/news/url?sa=t&ct3=MAA4CEgAUABgAWoCdXN6AWjYAQE&usg=AFQjCNGUV1AFw0LsmkcQzNnvo69ma_dhhA&rt=HOMEPAGE&url=http://www.cnn.com/2010/US/11/30/wikileaks/?hpt%3DSbin');void(0);  

I have tried all sorts of methods of escaping the String in Javascript like percent escaping and URIEncoding/URIDecoding but it does not seem to work, just keeps sending the escaped string instead of the actual characters.

Any help would be appreciated!


You might be able to make use of CFStringTransform if you replace the "\x" escapes with "\u00" escapes:

NSMutableString* js = [ [webView stringByEvaluatingJavaScriptFromString:
                                   [NSString stringWithFormat:
                                               @"SomeFunction(%i,%i);",
                                               (NSInteger)pt.x,(NSInteger)pt.y]
                         ] mutableCopy];
[js replaceOccurrencesOfString:@"\\x" 
                    withString:@"\\u00" 
                       options:NSCaseInsensitiveSearch 
                         range:NSMakeRange(0, [js length])];
CFStringTransform((CFMutableStringRef)js,
                  NULL, 
                  (CFStringRef)@"Any-Hex/Java", 
                  true);

I'm not certain which (if any) iOS versions support the "Any-Hex/Java" transliterator. Additionally, the above won't handle some strings properly; any substring with an even number of backslashes before the "x" will be erroneously decoded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜