Browse Source

feat(android): polyfill applicationNameForUserAgent on Android (#707)

Raphael Eidus 5 years ago
parent
commit
9c592d621c

+ 30
- 2
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -117,6 +117,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
117 117
 
118 118
   protected RNCWebChromeClient mWebChromeClient = null;
119 119
   protected boolean mAllowsFullscreenVideo = false;
120
+  protected @Nullable String mUserAgent = null;
121
+  protected @Nullable String mUserAgentWithApplicationName = null;
120 122
 
121 123
   public RNCWebViewManager() {
122 124
     mWebViewConfig = new WebViewConfig() {
@@ -298,8 +300,34 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
298 300
   @ReactProp(name = "userAgent")
299 301
   public void setUserAgent(WebView view, @Nullable String userAgent) {
300 302
     if (userAgent != null) {
301
-      // TODO(8496850): Fix incorrect behavior when property is unset (uA == null)
302
-      view.getSettings().setUserAgentString(userAgent);
303
+      mUserAgent = userAgent;
304
+    } else {
305
+      mUserAgent = null;
306
+    }
307
+    this.setUserAgentString(view);
308
+  }
309
+
310
+  @ReactProp(name = "applicationNameForUserAgent")
311
+  public void setApplicationNameForUserAgent(WebView view, @Nullable String applicationName) {
312
+    if(applicationName != null) {
313
+      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
314
+        String defaultUserAgent = WebSettings.getDefaultUserAgent(view.getContext());
315
+        mUserAgentWithApplicationName = defaultUserAgent + " " + applicationName;
316
+      }
317
+    } else {
318
+      mUserAgentWithApplicationName = null;
319
+    }
320
+    this.setUserAgentString(view);
321
+  }
322
+
323
+  protected void setUserAgentString(WebView view) {
324
+    if(mUserAgent != null) {
325
+      view.getSettings().setUserAgentString(mUserAgent);
326
+    } else if(mUserAgentWithApplicationName != null) {
327
+      view.getSettings().setUserAgentString(mUserAgentWithApplicationName);
328
+    } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
329
+      // handle unsets of `userAgent` prop as long as device is >= API 17
330
+      view.getSettings().setUserAgentString(WebSettings.getDefaultUserAgent(view.getContext()));
303 331
     }
304 332
   }
305 333
 

+ 12
- 2
docs/Reference.md View File

@@ -613,11 +613,21 @@ Sets the user-agent for the `WebView`. This will only work for iOS if you are us
613 613
 
614 614
 ### `applicationNameForUserAgent`
615 615
 
616
-Append to the existing user-agent. Available on iOS WKWebView only. Setting `userAgent` will override this.
616
+Append to the existing user-agent. This will only work for iOS if you are using WKWebView, not UIWebView. Setting `userAgent` will override this.
617 617
 
618 618
 | Type   | Required | Platform      |
619 619
 | ------ | -------- | ------------- |
620
-| string | No       | iOS WKWebView |
620
+| string | No       | Android, iOS WKWebView |
621
+
622
+```jsx
623
+<WebView
624
+  source={{ uri: 'https://facebook.github.io/react-native' }}
625
+  applicationNameForUserAgent={"DemoApp/1.1.0"}
626
+/>
627
+// Resulting User-Agent will look like:
628
+// 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
629
+// 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
630
+```
621 631
 
622 632
 ### `allowsFullscreenVideo`
623 633
 

+ 4
- 6
src/WebViewTypes.ts View File

@@ -230,6 +230,10 @@ export interface CommonNativeWebViewProps extends ViewProps {
230 230
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
231 231
   source: any;
232 232
   userAgent?: string;
233
+  /**
234
+   * Append to the existing user-agent. Overriden if `userAgent` is set.
235
+   */
236
+  applicationNameForUserAgent?: string;
233 237
 }
234 238
 
235 239
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
@@ -378,12 +382,6 @@ export interface IOSWebViewProps extends WebViewSharedProps {
378 382
    */
379 383
   useSharedProcessPool?: boolean;
380 384
 
381
-  /**
382
-   * Append to the existing user-agent. Overriden if `userAgent` is set.
383
-   * @platform ios
384
-   */
385
-  applicationNameForUserAgent?: string;
386
-
387 385
   /**
388 386
    * The custom user agent string.
389 387
    */