Send parameters to a local file in a UIWebView

萝らか妹 提交于 2019-12-02 19:36:05

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];

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];

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;

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.

No, unfortunately not.

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

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

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