Ios – Send parameters to a local file in a UIWebView

iosiphoneobjective cuiwebview

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!

Best Answer

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