Send parameters to a local file in a UIWebView

帅比萌擦擦* 提交于 2019-12-20 09:40:33

问题


I'm working on a native iPhone app. I am able to load a local file index.html into a UIWebView:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

The web loads fine. Now, I would like to load the web with some parameters, in the same way one types this in the browser: URL?var1=myValue&var2=myValue2

So I can get those values inside the javascript of the html file. Is there any way to send parameters to a local html file?

Thanks!


回答1:


The best and really working answer, only in objC (remember to retain your folder structure - copy files into xcode using "Create Folder References for any added folders")

NSString *path = [[NSBundle mainBundle]
       pathForResource:@"YOUR-HTML-FILE"
       ofType:@"html"
       inDirectory:@"YOUR-FOLDER" ]; 
 NSURL *url = [NSURL fileURLWithPath:path];



 NSString *theAbsoluteURLString = [url absoluteString];   

 NSString *queryString = @"?var=1232123232"; 

 NSString *absoluteURLwithQueryString = [theAbsoluteURLString stringByAppendingString: queryString];  

 NSURL *finalURL = [NSURL URLWithString: absoluteURLwithQueryString];

NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];

[web loadRequest:request];



回答2:


Marjan's way is the proper way of making it work.

As my friend Terence pointed it out, the trick is to use absolute file uri (file://) string when creating the url from string properly. This way ? character does not get encoded, and webview can load the file properly.

[NSURL URLWithString: @"file://localhost/Users/sj/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/../PrintingWebView.app/final.html?slide=2"];

Or use URLWithString:relativeToURL method

NSURL *relativeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSURL *u = [NSURL URLWithString:@"html/index.html?site=2" relativeToURL:relativeURL];



回答3:


You are loading the HTML from your own bundle which implies you have full control over it. Why not just parameterize the HTML with the fields you want. You could easily add in some hidden input fields that your javascript could then access. Say your HTML looked like this:

<body>
<form>
Static Field 1: <input type="text" name="field1"><br>
Static Field 2: <input type="text" name="field2"><br>
%@
</form>
</body>

Now just read this data into a string and format the %@ with some hidden fields like this:

NSString *input = @"<input type=\"hidden\" name=\"field3\"><br>";
NSString *formatted = [NSString stringWithFormat:html, input];

Then call -loadHTMLString: in the webview.

[webView loadHTMLString:formatted baseURL:nil];

Is there any reason you couldn't do it that way. Now your javascript can grab the value of the hidden input field with a call to:

document.getElementsByName('field3')[0].value;



回答4:


You can insert in your local html-file a javascript to parse the GET-request

<script language="javascript">
function getget(name) {
        var q = document.location.search;
        var i = q.indexOf(name + '=');

        if (i == -1) {
            return false;
        }

        var r = q.substr(i + name.length + 1, q.length - i - name.length - 1);

        i = r.indexOf('&');

        if (i != -1) {
            r = r.substr(0, i);
        }

        return r.replace(/\+/g, ' ');
    }
</script>

Use getget('parameter_name') to get the parameter from request string.




回答5:


No, unfortunately not.

You can, however, get around this by setting variables and calling functions / methods in JavaScript using the UIWebView:stringByEvaluatingJavaScriptFromString:




回答6:


Then call -loadHTMLString: in the webview.

I believe he was avoiding using loadHTMLString instead of loadRequest because it breaks back and forward buttons of the UIWebView



来源:https://stackoverflow.com/questions/1226363/send-parameters-to-a-local-file-in-a-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!