Hi I replaced UIWebview with WKWebview. Because on multiple frame loading UIWebview keyborad is dismissing. So i used WKWebView. my issue was gone now.
But on WKWebView im not getting cookies. it is returning only JSESSIONID cookie, Where as on UIWebView im getting all the cookies.
Please help me on this.
Here is my code snippet for start WKWebView.
func startWebView() {
URLCache.shared.removeAllCachedResponses()
if webView == nil {
let source =
"var meta = document.createElement('meta'); " +
"meta.name = 'viewport'; " +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'; " +
"var head = document.getElementsByTagName('head')[0]; " +
"head.appendChild(meta);"
let script = WKUserScript(source:source,
injectionTime: .atDocumentEnd,
forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.addUserScript(script)
let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController
let prefs = WKPreferences()
prefs.javaScriptEnabled = true
configuration.preferences = prefs
webView = WKWebView(frame: CGRect.zero, configuration: configuration)
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = true
webContainerView.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
let vdict = ["WV": webView!]
webContainerView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "H:|[WV]|",
options: [], metrics: nil,
views: vdict)
)
webContainerView.addConstraints(
NSLayoutConstraint.constraints(withVisualFormat: "V:|[WV]|",
options: [], metrics: nil,
views: vdict)
)
}
var urlstr = LoginServer.url.absoluteString
//#if DEBUG
let url : URL = URL(string: urlstr)!
var urlRequest = URLRequest(url: url,
cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData,
timeoutInterval: 60.0)
if Reachability.shared.isNetworkAvailable(){
urlRequest.cachePolicy = .reloadRevalidatingCacheData
_ = webView.load(urlRequest)
}
else{
loadingIndicator.stopAnimating()
loadingIndicator.isHidden = true
}
}
Here is the code for fetch cookies
func webView(_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Swift.Void)
{
if let httpResponse = navigationResponse.response as? HTTPURLResponse {
if let headers = httpResponse.allHeaderFields as? [String: String], let url = httpResponse.url {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: headers, for: url)
for cookie in cookies {
print(cookie.description)
print("found cookie " + cookie.name + " " + cookie.value)
}
}
}
decisionHandler(.allow)
}