Skip to content

Commit

Permalink
feat(ios): Add iOS contentMode property (react-native-webview#1538 by @…
Browse files Browse the repository at this point in the history
…TheAlmightyBob)

This allows overriding iPadOS 13's desktop-class browsing to load mobile content instead of desktop content.

Co-authored-by: Jamon Holmgren <[email protected]>
  • Loading branch information
TheAlmightyBob and jamonholmgren authored Aug 15, 2020
1 parent 22038cc commit 8b69452
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apple/RNCWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
@property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
@property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
@property (nonatomic, assign) WKContentMode contentMode;
#endif

+ (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential;
+ (void)setCustomCertificatesForHost:(nullable NSDictionary *)certificates;
- (void)postMessage:(NSString *_Nullable)message;
Expand Down
8 changes: 8 additions & 0 deletions apple/RNCWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ - (WKWebViewConfiguration *)setUpWkWebViewConfig
}
wkWebViewConfig.userContentController = [WKUserContentController new];

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
if (@available(iOS 13.0, *)) {
WKWebpagePreferences *pagePrefs = [[WKWebpagePreferences alloc]init];
pagePrefs.preferredContentMode = _contentMode;
wkWebViewConfig.defaultWebpagePreferences = pagePrefs;
}
#endif

// Shim the HTML5 history API:
[wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
name:HistoryShimName];
Expand Down
14 changes: 14 additions & 0 deletions apple/RNCWebViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
@interface RNCWebViewManager () <RNCWebViewDelegate>
@end

@implementation RCTConvert (WKWebView)
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
RCT_ENUM_CONVERTER(WKContentMode, (@{
@"recommended": @(WKContentModeRecommended),
@"mobile": @(WKContentModeMobile),
@"desktop": @(WKContentModeDesktop),
}), WKContentModeRecommended, integerValue)
#endif
@end

@implementation RNCWebViewManager
{
NSConditionLock *_shouldStartLoadLock;
Expand Down Expand Up @@ -70,6 +80,10 @@ - (RCTUIView *)view
RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
#endif

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
RCT_EXPORT_VIEW_PROPERTY(contentMode, WKContentMode)
#endif

/**
* Expose methods to enable messaging the webview.
*/
Expand Down
19 changes: 19 additions & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This document lays out the current public properties and methods for the React N
- [`overScrollMode`](Reference.md#overscrollmode)
- [`contentInset`](Reference.md#contentinset)
- [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
- [`contentMode`](Reference.md#contentMode)
- [`dataDetectorTypes`](Reference.md#datadetectortypes)
- [`scrollEnabled`](Reference.md#scrollenabled)
- [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
Expand Down Expand Up @@ -917,6 +918,24 @@ Possible values:

---

### `contentMode`[](#props-index)<!-- Link generated with jump2header -->

Controls the type of content to load. Available on iOS 13 and later. Defaults to `recommended`, which loads mobile content on iPhone & iPad Mini but desktop content on larger iPads.

See [Introducing Desktop-class Browsing on iPad](https://developer.apple.com/videos/play/wwdc2019/203/) for more.

Possible values:

- `recommended`
- `mobile`
- `desktop`

| Type | Required | Platform |
| ------ | -------- | -------- |
| string | No | iOS |

---

### `dataDetectorTypes`[](#props-index)<!-- Link generated with jump2header -->

Determines the types of data converted to clickable URLs in the web view's content. By default only phone numbers are detected.
Expand Down
15 changes: 15 additions & 0 deletions src/WebViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ export enum ContentInsetAdjustmentBehavior {
always = 'always'
};

export declare type ContentMode = 'recommended' | 'mobile' | 'desktop';

export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
allowingReadAccessToURL?: string;
allowsBackForwardNavigationGestures?: boolean;
Expand All @@ -308,6 +310,7 @@ export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
bounces?: boolean;
contentInset?: ContentInsetProp;
contentInsetAdjustmentBehavior?: ContentInsetAdjustmentBehavior;
contentMode?: ContentMode;
readonly dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
decelerationRate?: number;
directionalLockEnabled?: boolean;
Expand Down Expand Up @@ -405,6 +408,18 @@ export interface IOSWebViewProps extends WebViewSharedProps {
*/
contentInset?: ContentInsetProp;

/**
* Defaults to `recommended`, which loads mobile content on iPhone
* and iPad Mini but desktop content on other iPads.
*
* Possible values are:
* - `'recommended'`
* - `'mobile'`
* - `'desktop'`
* @platform ios
*/
contentMode?: ContentMode;

/**
* Determines the types of data converted to clickable URLs in the web view's content.
* By default only phone numbers are detected.
Expand Down

0 comments on commit 8b69452

Please sign in to comment.