Notalib / nativescript-webview-ext

Nativescript plugin with extended WebView functionality
Apache License 2.0
76 stars 37 forks source link

How to add headers on iOS #47

Open alonstar opened 5 years ago

alonstar commented 5 years ago

I need to add headers & set user-agent in WebView, I used nativescript-webview-utils for this issue before, but if I need to use WebViewExt for handling mailto, how do I add headers or set user-agent when using WebViewExt ?

Thank you.

Nativescript 5.4.0 iOS 11 Angular 7

m-abs commented 5 years ago

We don't support setting custom headers at the moment, and we don't have plans to add it. We might accept a PR, if implements the feature for all three platforms (android webview, WKWebView and UIWebView).

I've had a look at nativescript-webview-utils and I don't think we'd want to set custom headers that way.

User-agent should be possible, but requires a little code on your part.

Without having tested it:

if (isAndroid) {
   webview.android.getSettings().setUserAgentString(userAgent);
} else if (isIOS) {
  if (webview.isWKWebView) {
    webview.ios.customUserAgent = userAgent;
  } else if (webview.isUIWebView) {
    // Note: applies to every webview on the app.
    NSUserDefaults.standardUserDefaults.registerDefaults(NSDictionary.dictionaryWithObjectForKey(userAgent, "UserAgent"));
  }
}

If you need to capture mailto-links, you should be able to use shouldOverrideUrlLoading-event.

Again without testing:

webview.on(WebViewExt.shouldOverrideUrlLoading, (args) => {
  if (url.startsWith('mailto:')) {
    // DO STUFF
    args.cancel = true;
  }
});
alonstar commented 5 years ago

I got it , thank you 👍