Browse Source

feat(WKWebview): Add 'userAgent' property to the iOS WKWebView. (#112)

* Add 'userAgent' property to the iOS WKWebView

* Update 'userAgent' reference docs.
Bae Hyeonseung 5 years ago
parent
commit
2ec5fa514e
6 changed files with 12 additions and 3 deletions
  1. 2
    2
      docs/Reference.md
  2. 1
    0
      ios/RNCWKWebView.h
  3. 3
    1
      ios/RNCWKWebView.m
  4. 1
    0
      ios/RNCWKWebViewManager.m
  5. 1
    0
      js/WebView.ios.js
  6. 4
    0
      js/WebViewTypes.js

+ 2
- 2
docs/Reference.md View File

332
 
332
 
333
 ### `userAgent`
333
 ### `userAgent`
334
 
334
 
335
-Sets the user-agent for the `WebView`.
335
+Sets the user-agent for the `WebView`. This will only work for iOS if you are using WKWebView, not UIWebView (see https://developer.apple.com/documentation/webkit/wkwebview/1414950-customuseragent).
336
 
336
 
337
 | Type   | Required | Platform |
337
 | Type   | Required | Platform |
338
 | ------ | -------- | -------- |
338
 | ------ | -------- | -------- |
339
-| string | No       | Android  |
339
+| string | No       | Android, iOS WKWebView  |
340
 
340
 
341
 ---
341
 ---
342
 
342
 

+ 1
- 0
ios/RNCWKWebView.h View File

37
 @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
37
 @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
38
 @property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
38
 @property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
39
 @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
39
 @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
40
+@property (nonatomic, copy) NSString *userAgent;
40
 
41
 
41
 - (void)postMessage:(NSString *)message;
42
 - (void)postMessage:(NSString *)message;
42
 - (void)injectJavaScript:(NSString *)script;
43
 - (void)injectJavaScript:(NSString *)script;

+ 3
- 1
ios/RNCWKWebView.m View File

103
     _webView.scrollView.bounces = _bounces;
103
     _webView.scrollView.bounces = _bounces;
104
     [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
104
     [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
105
     _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures;
105
     _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures;
106
-
106
+    if (_userAgent) {
107
+      _webView.customUserAgent = _userAgent;
108
+    }
107
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
109
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
108
     if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
110
     if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
109
       _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
111
       _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

+ 1
- 0
ios/RNCWKWebViewManager.m View File

45
 RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
45
 RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
46
 RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
46
 RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
47
 RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
47
 RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
48
+RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
48
 
49
 
49
 /**
50
 /**
50
  * Expose methods to enable messaging the webview.
51
  * Expose methods to enable messaging the webview.

+ 1
- 0
js/WebView.ios.js View File

271
         }
271
         }
272
         hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView}
272
         hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView}
273
         allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures}
273
         allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures}
274
+        userAgent={this.props.userAgent}
274
         onLoadingStart={this._onLoadingStart}
275
         onLoadingStart={this._onLoadingStart}
275
         onLoadingFinish={this._onLoadingFinish}
276
         onLoadingFinish={this._onLoadingFinish}
276
         onLoadingError={this._onLoadingError}
277
         onLoadingError={this._onLoadingError}

+ 4
- 0
js/WebViewTypes.js View File

229
    * back-forward list navigations.
229
    * back-forward list navigations.
230
    */
230
    */
231
   allowsBackForwardNavigationGestures?: ?boolean,
231
   allowsBackForwardNavigationGestures?: ?boolean,
232
+  /**
233
+   * The custom user agent string.
234
+   */
235
+  userAgent?: ?string,
232
 |}>;
236
 |}>;
233
 
237
 
234
 export type AndroidWebViewProps = $ReadOnly<{|
238
 export type AndroidWebViewProps = $ReadOnly<{|