From ffc398d9e34cbd2b80de913e86b290ea75ad5fb3 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Fri, 16 Mar 2018 17:37:09 +0100 Subject: [PATCH] Add the original headers to the custom request (iOS only for now) #10 --- src/package.json | 2 +- src/webview-utils.ios.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/package.json b/src/package.json index 48e3610..17afd35 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-webview-utils", - "version": "2.0.2", + "version": "2.0.3", "description": "Add custom headers to a NativeScript WebView. Perhaps more utils later.", "main": "webview-utils", "typings": "index.d.ts", diff --git a/src/webview-utils.ios.ts b/src/webview-utils.ios.ts index c140b75..13129e4 100644 --- a/src/webview-utils.ios.ts +++ b/src/webview-utils.ios.ts @@ -34,6 +34,14 @@ class WebviewUtilsWKNavigationDelegateImpl extends NSObject implements WKNavigat timeoutInterval: 60 }); + // add the original headers + for (let i = 0; i < navigationAction.request.allHTTPHeaderFields.count; i++) { + const headerKey = navigationAction.request.allHTTPHeaderFields.allKeys.objectAtIndex(i); + customRequest.setValueForHTTPHeaderField( + navigationAction.request.allHTTPHeaderFields.objectForKey(headerKey), + headerKey); + } + this.headers.forEach((val, key) => { customRequest.setValueForHTTPHeaderField(val, key); // the user agent is set as header, but also set this property: @@ -148,6 +156,14 @@ export class WebViewUtils extends NSObject implements UIWebViewDelegate { timeoutInterval: 60 }); + // add the original headers + for (let i = 0; i < request.allHTTPHeaderFields.count; i++) { + const headerKey = request.allHTTPHeaderFields.allKeys.objectAtIndex(i); + nsMutableURLRequest.setValueForHTTPHeaderField( + request.allHTTPHeaderFields.objectForKey(headerKey), + headerKey); + } + WebViewUtils.headers.forEach((val, key) => { nsMutableURLRequest.setValueForHTTPHeaderField(val, key); if (key.toLowerCase() === "user-agent") {