Quellcode durchsuchen

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 vor 4 Jahren
Ursprung
Commit
8b69452643
Es ist kein Benutzerkonto mit dieser Commiter-Email verbunden
5 geänderte Dateien mit 60 neuen und 0 gelöschten Zeilen
  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 Datei anzeigen

@@ -63,6 +63,10 @@
63 63
 @property (nonatomic, assign) BOOL ignoreSilentHardwareSwitch;
64 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 70
 + (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential;
67 71
 + (void)setCustomCertificatesForHost:(nullable NSDictionary *)certificates;
68 72
 - (void)postMessage:(NSString *_Nullable)message;

+ 8
- 0
apple/RNCWebView.m Datei anzeigen

@@ -226,6 +226,14 @@ static NSDictionary* customCertificatesForHost;
226 226
   }
227 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 237
   // Shim the HTML5 history API:
230 238
   [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
231 239
                                                             name:HistoryShimName];

+ 14
- 0
apple/RNCWebViewManager.m Datei anzeigen

@@ -14,6 +14,16 @@
14 14
 @interface RNCWebViewManager () <RNCWebViewDelegate>
15 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 27
 @implementation RNCWebViewManager
18 28
 {
19 29
   NSConditionLock *_shouldStartLoadLock;
@@ -70,6 +80,10 @@ RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
70 80
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
71 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 88
  * Expose methods to enable messaging the webview.
75 89
  */

+ 19
- 0
docs/Reference.md Datei anzeigen

@@ -45,6 +45,7 @@ This document lays out the current public properties and methods for the React N
45 45
 - [`overScrollMode`](Reference.md#overscrollmode)
46 46
 - [`contentInset`](Reference.md#contentinset)
47 47
 - [`contentInsetAdjustmentBehavior`](Reference.md#contentInsetAdjustmentBehavior)
48
+- [`contentMode`](Reference.md#contentMode)
48 49
 - [`dataDetectorTypes`](Reference.md#datadetectortypes)
49 50
 - [`scrollEnabled`](Reference.md#scrollenabled)
50 51
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
@@ -917,6 +918,24 @@ Possible values:
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 939
 ### `dataDetectorTypes`[⬆](#props-index)<!-- Link generated with jump2header -->
921 940
 
922 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 Datei anzeigen

@@ -299,6 +299,8 @@ export enum ContentInsetAdjustmentBehavior {
299 299
   always = 'always'
300 300
 };
301 301
 
302
+export declare type ContentMode = 'recommended' | 'mobile' | 'desktop';
303
+
302 304
 export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
303 305
   allowingReadAccessToURL?: string;
304 306
   allowsBackForwardNavigationGestures?: boolean;
@@ -308,6 +310,7 @@ export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
308 310
   bounces?: boolean;
309 311
   contentInset?: ContentInsetProp;
310 312
   contentInsetAdjustmentBehavior?: ContentInsetAdjustmentBehavior;
313
+  contentMode?: ContentMode;
311 314
   readonly dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
312 315
   decelerationRate?: number;
313 316
   directionalLockEnabled?: boolean;
@@ -405,6 +408,18 @@ export interface IOSWebViewProps extends WebViewSharedProps {
405 408
    */
406 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 424
    * Determines the types of data converted to clickable URLs in the web view's content.
410 425
    * By default only phone numbers are detected.