Can't set headers on my WKWebView POST request

前端 未结 7 1357
花落未央
花落未央 2020-12-04 12:50

I want to do a POST request to my WKWebView but the headers doesn\'t get set when I monitor the requests with Charles so the request fails. What is wrong here?<

7条回答
  •  温柔的废话
    2020-12-04 13:28

    I use this delegate method and it works !!!

    #pragma mark - WKNavigationDelegate
    
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
        NSLog(@"%@",navigationAction.request.allHTTPHeaderFields);
    
        NSString *accessToken = @"Bearer 527d3401f16a8a7955aeae62299dbfbd";
        NSMutableURLRequest *request = [navigationAction.request mutableCopy];
    
        if(![[request.allHTTPHeaderFields allKeys] containsObject:@"Authorization"]){
            [request setValue:accessToken forHTTPHeaderField:@"Authorization"];
    
            decisionHandler(WKNavigationActionPolicyCancel);
            [Helper hideProgressHUD];
            [webView loadRequest:request];
    
        } else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
    }
    

提交回复
热议问题