Search code examples
ntlmwkwebview

Swift WKWebView


how to handle NTLM authentication using WKWebView, the aim is to load a secured URL with credentials (Sharepoint Hosted application) similarly I wanted to add a key value pair to the http request any code sample using swift2.0 that handles my case will be appreciated .


Solution

  • I went through the documentation and I have found the solutions below is the equivalent of the method shouldSatratLoading etc..

    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
        print("  decidePolicyForNavigationAction.......")
        let headerArr = navigationAction.request.allHTTPHeaderFields?.keys.array
        let headerIsPresent =  headerArr?.contains(APP_HEADER_ID)
    
        if headerIsPresent! {
             decisionHandler(.Allow)
        }else{
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                dispatch_async(dispatch_get_main_queue(), {
                    let newRequest: NSMutableURLRequest = navigationAction.request as! NSMutableURLRequest
                    // set new header
                    newRequest.addValue(APP_HEADER_VALUE, forHTTPHeaderField:APP_HEADER_ID)
    
                    // reload the request
                    webView.loadRequest(newRequest)
                })
            })
          decisionHandler(.Cancel)
        }
    }
    

    If anyone needs more info please let me know