Pārlūkot izejas kodu

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

* Add 'userAgent' property to the iOS WKWebView

* Update 'userAgent' reference docs.
Bae Hyeonseung 6 gadus atpakaļ
vecāks
revīzija
2ec5fa514e

+ 2
- 2
docs/Reference.md Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

@@ -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<{|