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

前端 未结 5 1059
旧巷少年郎
旧巷少年郎 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 15:50

    I know its late but may help others for SWIFT 3.0

    let weburl = NSURL(string: "http://www.mywebsite.com")
        let request = NSMutableURLRequest(URL: weburl!)
        request.setValue("HEADER_VALUE", forHTTPHeaderField:"HEADER_NAME")
        myWebView.loadRequest(request)
    
    0 讨论(0)
  • 2021-02-06 15:57

    You have two options either create a NSMutableUrlRequest at the start and load that with webView loadReqest or take over the complete URL loading of your app with NSURLProtocol.

    The most easiest way is the first choice as its only one extra lines of code:

    [webView loadRequest:mRequest];
    

    The second choice uses NSURLProtocol to take over URL loading of your app. this involves registering your own solution using creating a concrete class. the main method to override is canonicalRequestForRequest.

    I suggest you take a look at these two tutorials NSNipster and raywenderlich for guides.

    0 讨论(0)
  • 2021-02-06 16:01

    Swift 5.2 Solution

    By making URLRequest & setValue

    @IBOutlet var wkWebView: WKWebView!
    let url = URL(string: "https://YouAreBest.com")
    var request = URLRequest(url: url!)
    request.setValue("1", forHTTPHeaderField:"userid")
    wkWebView.load(request)
    
    0 讨论(0)
  • 2021-02-06 16:15

    a pure Swift 4 compliance answer should be something like the following:

    if let anURL = URL(string: aString) {
        var aRequest = URLRequest(url: anURL, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 5.0)
        if let tmpToken = Network.shared.getAuthenticationToken() {
            aRequest.setValue(tmpToken, forHTTPHeaderField: "Authorization")
        }
        self.loadRequest(aRequest)
    }
    

    where cachePolicy, timeoutInterval are to be set following your needs, as well as the if let statement in order to get an hypothetical token to be inserted in the request.

    The relevant part here is the way you set additional parameters on the URLRequest. No needs to use a Mutable statement anymore. var is the you go on the URLRequest.

    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
提交回复
热议问题