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

* Add 'userAgent' property to the iOS WKWebView

* Update 'userAgent' reference docs.
This commit is contained in:
Bae Hyeonseung
2018-11-19 11:13:31 +01:00
committed by Thibault Malbranche
parent 24572d01a3
commit 2ec5fa514e
6 changed files with 12 additions and 3 deletions
+2 -2
View File
@@ -332,11 +332,11 @@ Boolean value to enable third party cookies in the `WebView`. Used on Android Lo
### `userAgent` ### `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 | | Type | Required | Platform |
| ------ | -------- | -------- | | ------ | -------- | -------- |
| string | No | Android | | string | No | Android, iOS WKWebView |
--- ---
+1
View File
@@ -37,6 +37,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets; @property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
@property (nonatomic, assign) BOOL hideKeyboardAccessoryView; @property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
@property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures; @property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
@property (nonatomic, copy) NSString *userAgent;
- (void)postMessage:(NSString *)message; - (void)postMessage:(NSString *)message;
- (void)injectJavaScript:(NSString *)script; - (void)injectJavaScript:(NSString *)script;
+3 -1
View File
@@ -103,7 +103,9 @@ static NSString *const MessageHanderName = @"ReactNative";
_webView.scrollView.bounces = _bounces; _webView.scrollView.bounces = _bounces;
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil]; [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
_webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures; _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 defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) { if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
+1
View File
@@ -45,6 +45,7 @@ RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL) RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL) RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL) RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
/** /**
* Expose methods to enable messaging the webview. * Expose methods to enable messaging the webview.
+1
View File
@@ -271,6 +271,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
} }
hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView} hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView}
allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures} allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures}
userAgent={this.props.userAgent}
onLoadingStart={this._onLoadingStart} onLoadingStart={this._onLoadingStart}
onLoadingFinish={this._onLoadingFinish} onLoadingFinish={this._onLoadingFinish}
onLoadingError={this._onLoadingError} onLoadingError={this._onLoadingError}
+4
View File
@@ -229,6 +229,10 @@ export type IOSWebViewProps = $ReadOnly<{|
* back-forward list navigations. * back-forward list navigations.
*/ */
allowsBackForwardNavigationGestures?: ?boolean, allowsBackForwardNavigationGestures?: ?boolean,
/**
* The custom user agent string.
*/
userAgent?: ?string,
|}>; |}>;
export type AndroidWebViewProps = $ReadOnly<{| export type AndroidWebViewProps = $ReadOnly<{|