From 2ec5fa514e0831a4f61b5e69e0853b3c42bb83f4 Mon Sep 17 00:00:00 2001 From: Bae Hyeonseung Date: Mon, 19 Nov 2018 19:13:31 +0900 Subject: [PATCH] feat(WKWebview): Add 'userAgent' property to the iOS WKWebView. (#112) * Add 'userAgent' property to the iOS WKWebView * Update 'userAgent' reference docs. --- docs/Reference.md | 4 ++-- ios/RNCWKWebView.h | 1 + ios/RNCWKWebView.m | 4 +++- ios/RNCWKWebViewManager.m | 1 + js/WebView.ios.js | 1 + js/WebViewTypes.js | 4 ++++ 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/Reference.md b/docs/Reference.md index 03c8ffd..fef0101 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -332,11 +332,11 @@ Boolean value to enable third party cookies in the `WebView`. Used on Android Lo ### `userAgent` -Sets the user-agent for the `WebView`. +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). | Type | Required | Platform | | ------ | -------- | -------- | -| string | No | Android | +| string | No | Android, iOS WKWebView | --- diff --git a/ios/RNCWKWebView.h b/ios/RNCWKWebView.h index 3678156..ea15bda 100644 --- a/ios/RNCWKWebView.h +++ b/ios/RNCWKWebView.h @@ -37,6 +37,7 @@ shouldStartLoadForRequest:(NSMutableDictionary *)request @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets; @property (nonatomic, assign) BOOL hideKeyboardAccessoryView; @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures; +@property (nonatomic, copy) NSString *userAgent; - (void)postMessage:(NSString *)message; - (void)injectJavaScript:(NSString *)script; diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index fc1817e..ce8635e 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -103,7 +103,9 @@ static NSString *const MessageHanderName = @"ReactNative"; _webView.scrollView.bounces = _bounces; [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil]; _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures; - + if (_userAgent) { + _webView.customUserAgent = _userAgent; + } #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */ if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) { _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; diff --git a/ios/RNCWKWebViewManager.m b/ios/RNCWKWebViewManager.m index d625f2f..2a1ce3a 100644 --- a/ios/RNCWKWebViewManager.m +++ b/ios/RNCWKWebViewManager.m @@ -45,6 +45,7 @@ RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets) RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL) RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL) RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL) +RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString) /** * Expose methods to enable messaging the webview. diff --git a/js/WebView.ios.js b/js/WebView.ios.js index e32bc7d..ae679dc 100644 --- a/js/WebView.ios.js +++ b/js/WebView.ios.js @@ -271,6 +271,7 @@ class WebView extends React.Component { } hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView} allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures} + userAgent={this.props.userAgent} onLoadingStart={this._onLoadingStart} onLoadingFinish={this._onLoadingFinish} onLoadingError={this._onLoadingError} diff --git a/js/WebViewTypes.js b/js/WebViewTypes.js index 3e204e0..a7bdbd7 100644 --- a/js/WebViewTypes.js +++ b/js/WebViewTypes.js @@ -229,6 +229,10 @@ export type IOSWebViewProps = $ReadOnly<{| * back-forward list navigations. */ allowsBackForwardNavigationGestures?: ?boolean, + /** + * The custom user agent string. + */ + userAgent?: ?string, |}>; export type AndroidWebViewProps = $ReadOnly<{|