Browse Source

feat(ios): Add iOS contentMode property (#1538 by @TheAlmightyBob)

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

Co-authored-by: Jamon Holmgren <jamonholmgren@gmail.com>
Caleb Clarke 4 years ago
parent
commit
8b69452643
No account linked to committer's email address
5 changed files with 60 additions and 0 deletions
  1. 4
    0
      apple/RNCWebView.h
  2. 8
    0
      apple/RNCWebView.m
  3. 14
    0
      apple/RNCWebViewManager.m
  4. 19
    0
      docs/Reference.md
  5. 15
    0
      src/WebViewTypes.ts

+ 4
- 0
apple/RNCWebView.h View File

63
 @property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
63
 @property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
64
 @property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
64
 @property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
65
 
65
 
66
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
67
+@property (nonatomic, assign) WKContentMode contentMode;
68
+#endif
69
+
66
 + (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential;
70
 + (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential;
67
 + (void)setCustomCertificatesForHost:(nullable NSDictionary *)certificates;
71
 + (void)setCustomCertificatesForHost:(nullable NSDictionary *)certificates;
68
 - (void)postMessage:(NSString *_Nullable)message;
72
 - (void)postMessage:(NSString *_Nullable)message;

+ 8
- 0
apple/RNCWebView.m View File

226
   }
226
   }
227
   wkWebViewConfig.userContentController = [WKUserContentController new];
227
   wkWebViewConfig.userContentController = [WKUserContentController new];
228
 
228
 
229
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
230
+  if (@available(iOS 13.0, *)) {
231
+    WKWebpagePreferences *pagePrefs = [[WKWebpagePreferences alloc]init];
232
+    pagePrefs.preferredContentMode = _contentMode;
233
+    wkWebViewConfig.defaultWebpagePreferences = pagePrefs;
234
+  }
235
+#endif
236
+
229
   // Shim the HTML5 history API:
237
   // Shim the HTML5 history API:
230
   [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
238
   [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
231
                                                             name:HistoryShimName];
239
                                                             name:HistoryShimName];

+ 14
- 0
apple/RNCWebViewManager.m View File

14
 @interface RNCWebViewManager () <RNCWebViewDelegate>
14
 @interface RNCWebViewManager () <RNCWebViewDelegate>
15
 @end
15
 @end
16
 
16
 
17
+@implementation RCTConvert (WKWebView)
18
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
19
+RCT_ENUM_CONVERTER(WKContentMode, (@{
20
+    @"recommended": @(WKContentModeRecommended),
21
+    @"mobile": @(WKContentModeMobile),
22
+    @"desktop": @(WKContentModeDesktop),
23
+}), WKContentModeRecommended, integerValue)
24
+#endif
25
+@end
26
+
17
 @implementation RNCWebViewManager
27
 @implementation RNCWebViewManager
18
 {
28
 {
19
   NSConditionLock *_shouldStartLoadLock;
29
   NSConditionLock *_shouldStartLoadLock;
70
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
80
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
71
 #endif
81
 #endif
72
 
82
 
83
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
84
+RCT_EXPORT_VIEW_PROPERTY(contentMode, WKContentMode)
85
+#endif
86
+
73
 /**
87
 /**
74
  * Expose methods to enable messaging the webview.
88
  * Expose methods to enable messaging the webview.
75
  */
89
  */

+ 19
- 0
docs/Reference.md View File

45
 - [`overScrollMode`](Reference.md#overscrollmode)
45
 - [`overScrollMode`](Reference.md#overscrollmode)
46
 - [`contentInset`](Reference.md#contentinset)
46
 - [`contentInset`](Reference.md#contentinset)
47
 - [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
47
 - [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
48
+- [`contentMode`](Reference.md#contentMode)
48
 - [`dataDetectorTypes`](Reference.md#datadetectortypes)
49
 - [`dataDetectorTypes`](Reference.md#datadetectortypes)
49
 - [`scrollEnabled`](Reference.md#scrollenabled)
50
 - [`scrollEnabled`](Reference.md#scrollenabled)
50
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
51
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
917
 
918
 
918
 ---
919
 ---
919
 
920
 
921
+### `contentMode`[⬆](#props-index)<!-- Link generated with jump2header -->
922
+
923
+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.
924
+
925
+See [Introducing Desktop-class Browsing on iPad](https://developer.apple.com/videos/play/wwdc2019/203/) for more.
926
+
927
+Possible values:
928
+
929
+- `recommended`
930
+- `mobile`
931
+- `desktop`
932
+
933
+| Type   | Required | Platform |
934
+| ------ | -------- | -------- |
935
+| string | No       | iOS      |
936
+
937
+---
938
+
920
 ### `dataDetectorTypes`[⬆](#props-index)<!-- Link generated with jump2header -->
939
 ### `dataDetectorTypes`[⬆](#props-index)<!-- Link generated with jump2header -->
921
 
940
 
922
 Determines the types of data converted to clickable URLs in the web view's content. By default only phone numbers are detected.
941
 Determines the types of data converted to clickable URLs in the web view's content. By default only phone numbers are detected.

+ 15
- 0
src/WebViewTypes.ts View File

299
   always = 'always'
299
   always = 'always'
300
 };
300
 };
301
 
301
 
302
+export declare type ContentMode = 'recommended' | 'mobile' | 'desktop';
303
+
302
 export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
304
 export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
303
   allowingReadAccessToURL?: string;
305
   allowingReadAccessToURL?: string;
304
   allowsBackForwardNavigationGestures?: boolean;
306
   allowsBackForwardNavigationGestures?: boolean;
308
   bounces?: boolean;
310
   bounces?: boolean;
309
   contentInset?: ContentInsetProp;
311
   contentInset?: ContentInsetProp;
310
   contentInsetAdjustmentBehavior?: ContentInsetAdjustmentBehavior;
312
   contentInsetAdjustmentBehavior?: ContentInsetAdjustmentBehavior;
313
+  contentMode?: ContentMode;
311
   readonly dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
314
   readonly dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
312
   decelerationRate?: number;
315
   decelerationRate?: number;
313
   directionalLockEnabled?: boolean;
316
   directionalLockEnabled?: boolean;
405
    */
408
    */
406
   contentInset?: ContentInsetProp;
409
   contentInset?: ContentInsetProp;
407
 
410
 
411
+  /**
412
+   * Defaults to `recommended`, which loads mobile content on iPhone
413
+   * and iPad Mini but desktop content on other iPads.
414
+   * 
415
+   * Possible values are:
416
+   * - `'recommended'`
417
+   * - `'mobile'`
418
+   * - `'desktop'`
419
+   * @platform ios
420
+   */
421
+  contentMode?: ContentMode;
422
+
408
   /**
423
   /**
409
    * Determines the types of data converted to clickable URLs in the web view's content.
424
    * Determines the types of data converted to clickable URLs in the web view's content.
410
    * By default only phone numbers are detected.
425
    * By default only phone numbers are detected.