1

I have codes to load local html files (html files are inside the app, not from server), but how would I go about adding query string to it. I basically want to pass data from swift into html webview. Is this even possible? Alot of examples I've found is related to html files from server, but haven't found any for html files stored inside the app.

let url = NSBundle.mainBundle().URLForResource("index", withExtension:"html", subdirectory: "www")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)

5 Answers 5

3

To add query string parameters to local URL for WebView you can use code below. It is valid for Xcode 9 and Swift 4.

let bundleMainUrl = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www");
let fullUrl = URL(string: "?os=ios", relativeTo: bundleMainUrl);
let request = URLRequest(url: fullUrl!);
webView?.load(request);
Sign up to request clarification or add additional context in comments.

2 Comments

This is the only approach that worked for me on both iOS 11 and iOS 13.
Thanks, Mikep, This is the only solution work for me, +1
1

Yes you can do it by using Javascript. Check out stringByEvaluatingJavaScriptFromString

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString:

Comments

1

If you have a full URL string with the queries, you can just initialise NSURL with that string.

let url = NSURL(string: "www.stackoverflow.com")

1 Comment

My html are stored locally inside the app, not from the server.
1

You can use NSURLComponents and NSURLQueryItem to construct a URL with query parameters:

var urlComponents = NSURLComponents(string: "www/index.html")!

urlComponents.queryItems = [
  NSURLQueryItem(name: "key1", value: "value1"),
  NSURLQueryItem(name: "key2", value: "value2")
]
urlComponents.URL // returns www/index.html?key1=value1&key2=value2

6 Comments

The html files are stored locally on the app (not from server). So, if I use webView.loadRequest(urlComponents.URL) , i get an error message about expecting NSURLRequest type
Add the query items to the bundle's URL before passing it to NSURLRequest.
Not sure if I follow...can you provide the full source code to load local html files (index.html is stored inside a folder name WWW) with query string?
I showed you how to add query items to a URL. Break the URL you already have (from the bundle) down into its components, add the query items, then turn those components back into a URL.
Do you mean something like this? let urlComponents = NSURLComponents(string: "www/index.html")! if #available(iOS 8.0, *) { urlComponents.queryItems = [ NSURLQueryItem(name: "os", value: "guile"), NSURLQueryItem(name: "appID", value: "hi") ] } else { // Fallback on earlier versions } let request = NSURLRequest(URL: urlComponents.URL!) webView.loadRequest(request) This code doesn't work.
|
1

I've solved this by using pathForResource instead of pathForResource as I was able to append string toward the end.

var url = NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory: "www").stringByAppendingString("?os=ios")

1 Comment

It is not working for me because url is string and it can not be passed to webView load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.