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

前端 未结 5 1058
旧巷少年郎
旧巷少年郎 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: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.

提交回复
热议问题