Send parameters to a local file in a UIWebView

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:

  • How to directly rotate CVImageBuffer image in IOS 4 without converting to UIImage?
  • iOS - loading xib from bundle in Documents directory
  • How to change UITableViewCell appearance if cellForRowAtIndexPath is not calling
  • Custom URL scheme not recognized as link in SMS app(only in iphone 4 iOS 5.0)
  • Obtaining an NSDecimalNumber from a locale specific string?
  • Need assistance regarding transitionFromView:toView:duration:options:completion:
  • 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?


    6 Solutions Collect From Internet About “Send parameters to a local file in a UIWebView”

    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]
           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/../"];

    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:

    Static Field 1: <input type="text" name="field1"><br>
    Static Field 2: <input type="text" name="field2"><br>

    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:


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

    <script language="javascript">
    function getget(name) {
            var q =;
            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, ' ');

    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