11import { WebView } from "tns-core-modules/ui/web-view" ;
22
3+ class WebviewUtilsWKNavigationDelegateImpl extends NSObject implements WKNavigationDelegate {
4+ private headers : Map < string , string > ;
5+ public static ObjCProtocols = [ WKNavigationDelegate ] ;
6+
7+ public static initWithOwnerAndHeaders ( owner : WeakRef < WebView > , headers : Map < string , string > ) : WebviewUtilsWKNavigationDelegateImpl {
8+ const handler = < WebviewUtilsWKNavigationDelegateImpl > WebviewUtilsWKNavigationDelegateImpl . new ( ) ;
9+ handler . _owner = owner ;
10+ handler . headers = headers ;
11+ return handler ;
12+ }
13+ private _owner : WeakRef < WebView > ;
14+
15+ webViewDecidePolicyForNavigationActionDecisionHandler ( webView : WKWebView , navigationAction : WKNavigationAction , decisionHandler : ( p1 : WKNavigationActionPolicy ) => void ) : void {
16+ if ( ! navigationAction . request . URL ) {
17+ return ;
18+ }
19+
20+ let areHeadersAdded = true ;
21+ this . headers . forEach ( ( val , key ) => {
22+ areHeadersAdded = areHeadersAdded && navigationAction . request . valueForHTTPHeaderField ( key ) === val ;
23+ } ) ;
24+
25+ if ( ! areHeadersAdded ) {
26+ decisionHandler ( WKNavigationActionPolicy . Cancel ) ;
27+ const customRequest = new NSMutableURLRequest ( {
28+ URL : navigationAction . request . URL ,
29+ cachePolicy : NSURLRequestCachePolicy . UseProtocolCachePolicy ,
30+ timeoutInterval : 60
31+ } ) ;
32+
33+ this . headers . forEach ( ( val , key ) => {
34+ if ( key . toLowerCase ( ) === "user-agent" ) {
35+ webView . customUserAgent = val ;
36+ } else {
37+ customRequest . setValueForHTTPHeaderField ( val , key ) ;
38+ }
39+ } ) ;
40+
41+ webView . loadRequest ( customRequest ) ;
42+ } else {
43+ decisionHandler ( WKNavigationActionPolicy . Allow ) ;
44+ }
45+ }
46+ }
47+
348export class WebViewUtils extends NSObject implements UIWebViewDelegate {
449 public static ObjCProtocols = [ UIWebViewDelegate ] ;
550
@@ -11,14 +56,34 @@ export class WebViewUtils extends NSObject implements UIWebViewDelegate {
1156 private _originalDelegate : any ; // UIWebViewDelegateImpl
1257
1358 public static setUserAgent ( wv : WebView , userAgent : string ) {
14- // note that overrides the useragent for ALL webviews for the app, but that's prolly not a problem
15- NSUserDefaults . standardUserDefaults . registerDefaults (
16- NSDictionary . dictionaryWithObjectForKey ( userAgent , "UserAgent" ) ) ;
59+ if ( WebViewUtils . isWKWebView ( wv ) ) {
60+ const headers : Map < string , string > = new Map ( ) ;
61+ headers . set ( "User-Agent" , userAgent ) ;
62+ ( < WKWebView > wv . ios ) . navigationDelegate = ( < any > wv ) . _delegate = WebviewUtilsWKNavigationDelegateImpl . initWithOwnerAndHeaders ( new WeakRef ( wv ) , headers ) ;
63+ } else {
64+ // note that this overrides the useragent for ALL webviews for the app, but that's prolly not a problem
65+ NSUserDefaults . standardUserDefaults . registerDefaults (
66+ NSDictionary . dictionaryWithObjectForKey ( userAgent , "UserAgent" ) ) ;
67+ }
1768 }
1869
1970 public static addHeaders ( wv : WebView , headers : Map < string , string > ) {
20- ( < any > wv ) . _delegate = WebViewUtils . initWithOwner ( new WeakRef ( wv ) ) ;
21- WebViewUtils . headers = headers ;
71+ if ( WebViewUtils . isWKWebView ( wv ) ) {
72+ ( < WKWebView > wv . ios ) . navigationDelegate = ( < any > wv ) . _delegate = WebviewUtilsWKNavigationDelegateImpl . initWithOwnerAndHeaders ( new WeakRef ( wv ) , headers ) ;
73+ } else {
74+ ( < any > wv ) . _delegate = WebViewUtils . initWithOwner ( new WeakRef ( wv ) ) ;
75+ WebViewUtils . headers = headers ;
76+ }
77+ }
78+
79+ /**
80+ * NativeScript < 3.4 used UIWebView. Newer versions use WKWebView.
81+ * So one day the UIWebView code can be removed.
82+ * @param {WebView } wv
83+ * @returns {boolean }
84+ */
85+ private static isWKWebView ( wv : WebView ) : boolean {
86+ return wv . ios . isKindOfClass ( WKWebView . class ( ) ) ;
2287 }
2388
2489 private static initWithOwner ( owner : WeakRef < WebView > ) : WebViewUtils {
@@ -28,8 +93,7 @@ export class WebViewUtils extends NSObject implements UIWebViewDelegate {
2893 return delegate ;
2994 }
3095
31- // TODO this is prolly different for WKWebView
32- // You can customize your http headers here.
96+ // UIWebView delegate
3397 public webViewShouldStartLoadWithRequestNavigationType ( webView : UIWebView , request : NSURLRequest , navigationType : number ) {
3498 const urlString : string = request . URL . absoluteString ;
3599 const isNavigationTypeBackForward = navigationType === UIWebViewNavigationType . BackForward ;
@@ -57,14 +121,17 @@ export class WebViewUtils extends NSObject implements UIWebViewDelegate {
57121 return false ;
58122 }
59123
124+ // UIWebView delegate
60125 public webViewDidStartLoad ( webView : UIWebView ) {
61126 this . _originalDelegate . webViewDidStartLoad ( webView ) ;
62127 }
63128
129+ // UIWebView delegate
64130 public webViewDidFinishLoad ( webView : UIWebView ) {
65131 this . _originalDelegate . webViewDidFinishLoad ( webView ) ;
66132 }
67133
134+ // UIWebView delegate
68135 public webViewDidFailLoadWithError ( webView : UIWebView , error : NSError ) {
69136 this . _originalDelegate . webViewDidFailLoadWithError ( webView , error ) ;
70137 }
0 commit comments