How to add customize HTTP headers in UIWebView request, my UIWebView is based on Cordova project?

前端 未结 5 1114
旧巷少年郎
旧巷少年郎 2021-02-06 15:22

My iOS UIWebView page is based on Cordova open source framework, and I want to add some customize http headers in its webview URL request, my solution is to add them in the foll

5条回答
  •  没有蜡笔的小新
    2021-02-06 16:16

    Sometimes Cookies are not set even after you assign all http headers. it is better to create mutable request and copy your nsurlrequest and add your custom header to it so that all information from original request is retained in mutable one.

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
    {
    
      if(check if key not present){
      NSMutableURLRequest *re = [[NSMutableURLRequest alloc] init];//alloc init      not required
      re = (NSMutableURLRequest *) request.mutableCopy;
      [re setValue:@"Your Custom Value" forHTTPHeaderField:@"Yout Custom     Header"];
          [webView loadRequest:re] ;
      return NO;
    
      }
    return YES;
    
    }
    

提交回复
热议问题