feat(android): polyfill applicationNameForUserAgent on Android (#707)
This commit is contained in:
parent
2333e45208
commit
9c592d621c
|
@ -117,6 +117,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
|
|||
|
||||
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<WebView> {
|
|||
@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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
<WebView
|
||||
source={{ uri: 'https://facebook.github.io/react-native' }}
|
||||
applicationNameForUserAgent={"DemoApp/1.1.0"}
|
||||
/>
|
||||
// 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`
|
||||
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue