简体   繁体   English

如何将参数传递给fileURL

[英]How to pass arguments to fileURL

I'm trying to pass arguments in NSURL of file path in the usual way: 我试图以通常的方式在文件路径的NSURL中传递参数:

(fileURL)?arg1=val1&arg2=val2

The problem is that I keep getting the following error from the UIWebView : 问题是我不断从UIWebView获得以下错误:

Failed to load webpage with error: The requested URL was not found on this server.

When I remove the arguments suffix everything works perfect. 当我删除参数后缀时,一切正常。 The documentation on Apple state that the method 关于Apple的文档说明了该方法

+ (id)fileURLWithPath:(NSString *)path +(id)fileURLWithPath:(NSString *)路径

Works like this: "path: The path that the NSURL object will represent. path should be a valid system path..." 像这样工作: "path: The path that the NSURL object will represent. path should be a valid system path..."

Is it possible to add the arguments to fileURL? 是否可以将参数添加到fileURL? If so, how can I do it? 如果是这样,我该怎么办?

By the way, I succeeded to do so, if I try to load local URL file with arguments from the UIWebView with Javascript call: 顺便说一下,如果我尝试使用带有Javascript调用的UIWebView中的参数加载本地URL文件,我就成功了:

document.location = url;

I try to find the solution from native code... 我尝试从本机代码中找到解决方案...

Ok, I see your problem. 好的,我看到了你的问题。 You can't add the ?arg1=something&arg2=... to the path, because there could be a file named like that, so NSURL escapes the ? 你不能将?arg1=something&arg2=...到路径中,因为可能有一个名为like的文件,所以NSURL会逃避? character to ensure it's used as a path, not part of the query string (anything after the ? is called a query in a URL). 字符,以确保将其用作路径,而不是查询字符串的一部分( ?之后的任何内容在URL中称为查询)。

To do this properly, you need to set the URL by giving a properly escaped string. 要正确执行此操作,您需要通过提供正确转义的字符串来设置URL。

NSString *path = @"/Users/mike/file.html"; 
NSString *query = @"option1=5&option2=hello"; // for example

NSString *encodedPath = [path stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@?%@",
                                   encodedPath,
                                   query]];

Have you tried using the fragment instead? 您是否尝试过使用该片段? It's the part of the URL after the # symbol. 它是#符号后面的URL的一部分。 This part is never sent to the server, so shouldn't interfere with the path, but javascript on the page can still read it. 这部分永远不会发送到服务器,所以不应该干扰路径,但页面上的javascript仍然可以读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM