瀏覽代碼

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

* Add 'userAgent' property to the iOS WKWebView

* Update 'userAgent' reference docs.
Bae Hyeonseung 6 年之前
父節點
當前提交
2ec5fa514e
共有 6 個檔案被更改,包括 12 行新增3 行删除
  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 查看文件

@@ -332,11 +332,11 @@ Boolean value to enable third party cookies in the `WebView`. Used on Android Lo
332 332
 
333 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 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 查看文件

@@ -37,6 +37,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
37 37
 @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
38 38
 @property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
39 39
 @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
40
+@property (nonatomic, copy) NSString *userAgent;
40 41
 
41 42
 - (void)postMessage:(NSString *)message;
42 43
 - (void)injectJavaScript:(NSString *)script;

+ 3
- 1
ios/RNCWKWebView.m 查看文件

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

+ 1
- 0
ios/RNCWKWebViewManager.m 查看文件

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

+ 1
- 0
js/WebView.ios.js 查看文件

@@ -271,6 +271,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
271 271
         }
272 272
         hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView}
273 273
         allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures}
274
+        userAgent={this.props.userAgent}
274 275
         onLoadingStart={this._onLoadingStart}
275 276
         onLoadingFinish={this._onLoadingFinish}
276 277
         onLoadingError={this._onLoadingError}

+ 4
- 0
js/WebViewTypes.js 查看文件

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