Browse Source

feat(iOS): Add the pull to refresh (#1265)

* Add pull to refresh support for iOS

* Add pull to refresh control removal from WebView

* Add the type and reference description about pull to refresh

* Set bounces to true when enabling pull to refresh, add references

* Add the back to props anchor to pullToRefreshEnabled
Sergei Butko 4 years ago
parent
commit
a02d88f54f
No account linked to committer's email address
5 changed files with 65 additions and 5 deletions
  1. 4
    0
      apple/RNCWebView.h
  2. 36
    4
      apple/RNCWebView.m
  3. 4
    0
      apple/RNCWebViewManager.m
  4. 11
    0
      docs/Reference.md
  5. 10
    1
      src/WebViewTypes.ts

+ 4
- 0
apple/RNCWebView.h View File

62
 @property (nonatomic, assign) BOOL directionalLockEnabled;
62
 @property (nonatomic, assign) BOOL directionalLockEnabled;
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
+@property (nonatomic, assign) BOOL pullToRefreshEnabled;
66
+@property (nonatomic, weak) UIRefreshControl * refreshControl;
65
 
67
 
66
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
68
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
67
 @property (nonatomic, assign) WKContentMode contentMode;
69
 @property (nonatomic, assign) WKContentMode contentMode;
75
 - (void)goBack;
77
 - (void)goBack;
76
 - (void)reload;
78
 - (void)reload;
77
 - (void)stopLoading;
79
 - (void)stopLoading;
80
+- (void)addPullToRefreshControl;
81
+- (void)pullToRefresh:(UIRefreshControl *)refreshControl;
78
 
82
 
79
 @end
83
 @end

+ 36
- 4
apple/RNCWebView.m View File

275
     _webView.UIDelegate = self;
275
     _webView.UIDelegate = self;
276
     _webView.navigationDelegate = self;
276
     _webView.navigationDelegate = self;
277
 #if !TARGET_OS_OSX
277
 #if !TARGET_OS_OSX
278
+    if (_pullToRefreshEnabled) {
279
+        [self addPullToRefreshControl];
280
+    }
278
     _webView.scrollView.scrollEnabled = _scrollEnabled;
281
     _webView.scrollView.scrollEnabled = _scrollEnabled;
279
     _webView.scrollView.pagingEnabled = _pagingEnabled;
282
     _webView.scrollView.pagingEnabled = _pagingEnabled;
280
-    _webView.scrollView.bounces = _bounces;
283
+      //For UIRefreshControl to work correctly, the bounces should always be true
284
+    _webView.scrollView.bounces = _pullToRefreshEnabled || _bounces; 
281
     _webView.scrollView.showsHorizontalScrollIndicator = _showsHorizontalScrollIndicator;
285
     _webView.scrollView.showsHorizontalScrollIndicator = _showsHorizontalScrollIndicator;
282
     _webView.scrollView.showsVerticalScrollIndicator = _showsVerticalScrollIndicator;
286
     _webView.scrollView.showsVerticalScrollIndicator = _showsVerticalScrollIndicator;
283
     _webView.scrollView.directionalLockEnabled = _directionalLockEnabled;
287
     _webView.scrollView.directionalLockEnabled = _directionalLockEnabled;
308
   _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures;
312
   _webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures;
309
 }
313
 }
310
 
314
 
311
-
312
 - (void)removeFromSuperview
315
 - (void)removeFromSuperview
313
 {
316
 {
314
     if (_webView) {
317
     if (_webView) {
1156
   }
1159
   }
1157
 }
1160
 }
1158
 
1161
 
1162
+- (void)addPullToRefreshControl
1163
+{
1164
+    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
1165
+    _refreshControl = refreshControl;
1166
+    [_webView.scrollView addSubview: refreshControl];
1167
+    [refreshControl addTarget:self action:@selector(pullToRefresh:) forControlEvents: UIControlEventValueChanged];
1168
+}
1169
+
1170
+- (void)pullToRefresh:(UIRefreshControl *)refreshControl
1171
+{
1172
+    [self reload];
1173
+    [refreshControl endRefreshing];
1174
+}
1175
+
1176
+#if !TARGET_OS_OSX
1177
+- (void)setPullToRefreshEnabled:(BOOL)pullToRefreshEnabled
1178
+{
1179
+    _pullToRefreshEnabled = pullToRefreshEnabled;
1180
+    
1181
+    if (pullToRefreshEnabled) {
1182
+        [self addPullToRefreshControl];
1183
+    } else {
1184
+        [_refreshControl removeFromSuperview];
1185
+    }
1186
+
1187
+    [self setBounces:_bounces];
1188
+}
1189
+#endif // !TARGET_OS_OSX
1190
+
1159
 - (void)stopLoading
1191
 - (void)stopLoading
1160
 {
1192
 {
1161
   [_webView stopLoading];
1193
   [_webView stopLoading];
1165
 - (void)setBounces:(BOOL)bounces
1197
 - (void)setBounces:(BOOL)bounces
1166
 {
1198
 {
1167
   _bounces = bounces;
1199
   _bounces = bounces;
1168
-  _webView.scrollView.bounces = bounces;
1200
+    //For UIRefreshControl to work correctly, the bounces should always be true
1201
+  _webView.scrollView.bounces = _pullToRefreshEnabled || bounces;
1169
 }
1202
 }
1170
 #endif // !TARGET_OS_OSX
1203
 #endif // !TARGET_OS_OSX
1171
 
1204
 
1172
-
1173
 - (void)setInjectedJavaScript:(NSString *)source {
1205
 - (void)setInjectedJavaScript:(NSString *)source {
1174
   _injectedJavaScript = source;
1206
   _injectedJavaScript = source;
1175
 
1207
 

+ 4
- 0
apple/RNCWebViewManager.m View File

103
   }];
103
   }];
104
 }
104
 }
105
 
105
 
106
+RCT_CUSTOM_VIEW_PROPERTY(pullToRefreshEnabled, BOOL, RNCWebView) {
107
+    view.pullToRefreshEnabled = json == nil ? false : [RCTConvert BOOL: json];
108
+}
109
+
106
 RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWebView) {
110
 RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWebView) {
107
   view.bounces = json == nil ? true : [RCTConvert BOOL: json];
111
   view.bounces = json == nil ? true : [RCTConvert BOOL: json];
108
 }
112
 }

+ 11
- 0
docs/Reference.md View File

67
 - [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
67
 - [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
68
 - [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
68
 - [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
69
 - [`textZoom`](Reference.md#textZoom)
69
 - [`textZoom`](Reference.md#textZoom)
70
+- [`pullToRefreshEnabled`](Reference.md#pullToRefreshEnabled)
70
 - [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch)
71
 - [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch)
71
 - [`onFileDownload`](Reference.md#onFileDownload)
72
 - [`onFileDownload`](Reference.md#onFileDownload)
72
 
73
 
1193
 
1194
 
1194
 `<WebView textZoom={100} />`
1195
 `<WebView textZoom={100} />`
1195
 
1196
 
1197
+---
1198
+
1199
+### `pullToRefreshEnabled`[⬆](#props-index)<!-- Link generated with jump2header -->
1200
+
1201
+Boolean value that determines whether a pull to refresh gesture is available in the `WebView`. The default value is `false`. If `true`, sets `bounces` automatically to `true`.
1202
+
1203
+| Type    | Required | Platform |
1204
+| ------- | -------- | -------- |
1205
+| boolean | No       | iOS      |
1206
+
1196
 ### `ignoreSilentHardwareSwitch`[⬆](#props-index)<!-- Link generated with jump2header -->
1207
 ### `ignoreSilentHardwareSwitch`[⬆](#props-index)<!-- Link generated with jump2header -->
1197
 
1208
 
1198
 (ios only)
1209
 (ios only)

+ 10
- 1
src/WebViewTypes.ts View File

417
   /**
417
   /**
418
    * Defaults to `recommended`, which loads mobile content on iPhone
418
    * Defaults to `recommended`, which loads mobile content on iPhone
419
    * and iPad Mini but desktop content on other iPads.
419
    * and iPad Mini but desktop content on other iPads.
420
-   * 
420
+   *
421
    * Possible values are:
421
    * Possible values are:
422
    * - `'recommended'`
422
    * - `'recommended'`
423
    * - `'mobile'`
423
    * - `'mobile'`
551
   */
551
   */
552
   injectedJavaScriptBeforeContentLoadedForMainFrameOnly?: boolean;
552
   injectedJavaScriptBeforeContentLoadedForMainFrameOnly?: boolean;
553
 
553
 
554
+  /**
555
+   * Boolean value that determines whether a pull to refresh gesture is
556
+   * available in the `WebView`. The default value is `false`.
557
+   * If `true`, sets `bounces` automatically to `true`
558
+   * @platform ios
559
+   *
560
+  */
561
+  pullToRefreshEnabled?: boolean;
562
+
554
   /**
563
   /**
555
    * Function that is invoked when the client needs to download a file.
564
    * Function that is invoked when the client needs to download a file.
556
    *
565
    *