NSURL add parameters to fileURLWithPath method

Borut Tomazin picture Borut Tomazin · Aug 9, 2012 · Viewed 10.3k times · Source

I use this line of code to load a local html file into a web view:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"html"]];

However I want to add some http parameters to the url with no luck so far.

I've tried this:

url = [url URLByAppendingPathComponent:@"?param1=1"];

But after this a html doesn't load in webview.

Is there a way to load local html file in webview with params ?

Answer

Paresh Navadiya picture Paresh Navadiya · Aug 9, 2012

Do this:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"html"]];

NSString *URLString = [url absoluteString];   
NSString *queryString = @"?param1=1"; 
NSString *URLwithQueryString = [URLString stringByAppendingString: queryString];  

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

[web loadRequest:request];