[NSURL URLWithString:stringContainingSpecialCharacters]

发布于 2024-10-14 02:22:45 字数 1947 浏览 3 评论 0原文

假设我有一个地址栏,它是一个名为 textField 的 UITextField 和一个名为 webView 的 UIWebView。大多数情况下,以下代码有效:

[webView loadURL:[NSURL URLWithString:textField.text]];

当我输入一些带有特殊字符的长字符串时,URLWithString: 仅返回null。可读性书签就是一个很好的例子:

javascript:(function(){readConvertLinksToFootnotes=true;readStyle='style-newspaper';readSize='size-medium';readMargin='margin-wide';_readability_script=document.createElement('script');_readability_script.type='text/javascript';_readability_script.src='http://lab.arc90.com/experiments/readability/js/readability.js?x='+(Math.random());document.documentElement.appendChild(_readability_script);_readability_css=document.createElement('link');_readability_css.rel='stylesheet';_readability_css.href='http://lab.arc90.com/experiments/readability/css/readability.css';_readability_css.type='text/css';_readability_css.media='all';document.documentElement.appendChild(_readability_css);_readability_print_css=document.createElement('link');_readability_print_css.rel='stylesheet';_readability_print_css.href='http://lab.arc90.com/experiments/readability/css/readability-print.css';_readability_print_css.media='print';_readability_print_css.type='text/css';document.getElementsByTagName('head')[0].appendChild(_readability_print_css);})();

根据 这个答案,我可以使用stringByAddingPercentEscapesUsingEncoding来转义字符串,实际上它对于这种情况工作得很好。

我的问题是:在将字符串传递给 webView 之前始终调用 stringByAddingPercentEscapesUsingEncoding: 是否安全?以下情况有什么后果吗?

[webView loadURL:[NSURL URLWithString:[textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]

谢谢!

编辑:我所说的“安全”是指,虽然这适用于包含 Unicode 字符的 URL,但它仍然适用于“正常”URL 吗?

编辑2:如果这是“安全”的,为什么它不是默认行为?

Say, I have an address bar which is a UITextField called textField, and a UIWebView named webView. Most of the time the follow code works:

[webView loadURL:[NSURL URLWithString:textField.text]];

When I put in some long string with special characters, URLWithString: simply returns null. The Readability bookmarklet is a good example:

javascript:(function(){readConvertLinksToFootnotes=true;readStyle='style-newspaper';readSize='size-medium';readMargin='margin-wide';_readability_script=document.createElement('script');_readability_script.type='text/javascript';_readability_script.src='http://lab.arc90.com/experiments/readability/js/readability.js?x='+(Math.random());document.documentElement.appendChild(_readability_script);_readability_css=document.createElement('link');_readability_css.rel='stylesheet';_readability_css.href='http://lab.arc90.com/experiments/readability/css/readability.css';_readability_css.type='text/css';_readability_css.media='all';document.documentElement.appendChild(_readability_css);_readability_print_css=document.createElement('link');_readability_print_css.rel='stylesheet';_readability_print_css.href='http://lab.arc90.com/experiments/readability/css/readability-print.css';_readability_print_css.media='print';_readability_print_css.type='text/css';document.getElementsByTagName('head')[0].appendChild(_readability_print_css);})();

According to this answer, I can use stringByAddingPercentEscapesUsingEncoding to escape the string, and indeed it works fine for this case.

My question is: Is this safe to always call stringByAddingPercentEscapesUsingEncoding: before passing the string to the webView? Does the following have any consequences?

[webView loadURL:[NSURL URLWithString:[textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]

Thanks!

EDIT: What I mean by 'safe' is that while this works with URLs containing Unicode characters, does it still work fine with 'normal' URLs?

EDIT 2: If this is 'safe', why isn't it the default behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-10-21 02:22:45

由于我的问题没有其他答案,我决定使用以下 - (NSURL *)URLValue 方法而不是 - (NSURL *)URLWithString: 作为安全措施:

- (NSURL *)URLValue {
    NSURL *URL = [NSURL URLWithString:self];

    if (URL) return URL;

    return [NSURL URLWithString:[self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}

Since there's no other answers to my question, I decided to use the following - (NSURL *)URLValue method instead of - (NSURL *)URLWithString: as a safe measure:

- (NSURL *)URLValue {
    NSURL *URL = [NSURL URLWithString:self];

    if (URL) return URL;

    return [NSURL URLWithString:[self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
往昔成烟 2024-10-21 02:22:45

没问题。所有返回的对象都是自动释放的,因此不会泄漏。

No problems. All the returned objects are autoreleased, so no leaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文