diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java index f6aa91e..57b3a47 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java @@ -117,6 +117,8 @@ public class RNCWebViewManager extends SimpleViewManager { protected RNCWebChromeClient mWebChromeClient = null; protected boolean mAllowsFullscreenVideo = false; + protected @Nullable String mUserAgent = null; + protected @Nullable String mUserAgentWithApplicationName = null; public RNCWebViewManager() { mWebViewConfig = new WebViewConfig() { @@ -298,8 +300,34 @@ public class RNCWebViewManager extends SimpleViewManager { @ReactProp(name = "userAgent") public void setUserAgent(WebView view, @Nullable String userAgent) { if (userAgent != null) { - // TODO(8496850): Fix incorrect behavior when property is unset (uA == null) - view.getSettings().setUserAgentString(userAgent); + mUserAgent = userAgent; + } else { + mUserAgent = null; + } + this.setUserAgentString(view); + } + + @ReactProp(name = "applicationNameForUserAgent") + public void setApplicationNameForUserAgent(WebView view, @Nullable String applicationName) { + if(applicationName != null) { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + String defaultUserAgent = WebSettings.getDefaultUserAgent(view.getContext()); + mUserAgentWithApplicationName = defaultUserAgent + " " + applicationName; + } + } else { + mUserAgentWithApplicationName = null; + } + this.setUserAgentString(view); + } + + protected void setUserAgentString(WebView view) { + if(mUserAgent != null) { + view.getSettings().setUserAgentString(mUserAgent); + } else if(mUserAgentWithApplicationName != null) { + view.getSettings().setUserAgentString(mUserAgentWithApplicationName); + } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + // handle unsets of `userAgent` prop as long as device is >= API 17 + view.getSettings().setUserAgentString(WebSettings.getDefaultUserAgent(view.getContext())); } } diff --git a/docs/Reference.md b/docs/Reference.md index 67b02b2..66ae88c 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -613,11 +613,21 @@ Sets the user-agent for the `WebView`. This will only work for iOS if you are us ### `applicationNameForUserAgent` -Append to the existing user-agent. Available on iOS WKWebView only. Setting `userAgent` will override this. +Append to the existing user-agent. This will only work for iOS if you are using WKWebView, not UIWebView. Setting `userAgent` will override this. | Type | Required | Platform | | ------ | -------- | ------------- | -| string | No | iOS WKWebView | +| string | No | Android, iOS WKWebView | + +```jsx + +// Resulting User-Agent will look like: +// Mozilla/5.0 (Linux; Android 8.1.0; Android SDK built for x86 Build/OSM1.180201.021; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.98 Mobile Safari/537.36 DemoApp/1.1.0 +// Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 DemoApp/1.1.0 +``` ### `allowsFullscreenVideo` diff --git a/src/WebViewTypes.ts b/src/WebViewTypes.ts index e94f7df..4729a16 100644 --- a/src/WebViewTypes.ts +++ b/src/WebViewTypes.ts @@ -230,6 +230,10 @@ export interface CommonNativeWebViewProps extends ViewProps { // eslint-disable-next-line @typescript-eslint/no-explicit-any source: any; userAgent?: string; + /** + * Append to the existing user-agent. Overriden if `userAgent` is set. + */ + applicationNameForUserAgent?: string; } export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps { @@ -378,12 +382,6 @@ export interface IOSWebViewProps extends WebViewSharedProps { */ useSharedProcessPool?: boolean; - /** - * Append to the existing user-agent. Overriden if `userAgent` is set. - * @platform ios - */ - applicationNameForUserAgent?: string; - /** * The custom user agent string. */