Browse Source

feat(iOS): Add prop for allowingReadAccessToURL (#771)

Harry Yu 5 years ago
parent
commit
0424dd0801
5 changed files with 39 additions and 1 deletions
  1. 11
    0
      docs/Reference.md
  2. 1
    0
      ios/RNCWKWebView.h
  3. 13
    1
      ios/RNCWKWebView.m
  4. 1
    0
      ios/RNCWKWebViewManager.m
  5. 13
    0
      src/WebViewTypes.ts

+ 11
- 0
docs/Reference.md View File

@@ -42,6 +42,7 @@ This document lays out the current public properties and methods for the React N
42 42
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
43 43
 - [`geolocationEnabled`](Reference.md#geolocationenabled)
44 44
 - [`allowUniversalAccessFromFileURLs`](Reference.md#allowUniversalAccessFromFileURLs)
45
+- [`allowingReadAccessToURL`](Reference.md#allowingReadAccessToURL)
45 46
 - [`useWebKit`](Reference.md#usewebkit)
46 47
 - [`url`](Reference.md#url)
47 48
 - [`html`](Reference.md#html)
@@ -795,6 +796,16 @@ Boolean that sets whether JavaScript running in the context of a file scheme URL
795 796
 
796 797
 ---
797 798
 
799
+### `allowingReadAccessToURL`
800
+
801
+A String value that indicates which URLs the WebView's file can then reference in scripts, AJAX requests, and CSS imports. This is only used in `RNCWKWebView` for WebViews that are loaded with a source.uri set to a `'file://'` URL. If not provided, the default is to only allow read access to the URL provided in source.uri itself.
802
+
803
+| Type   | Required | Platform      |
804
+| ------ | -------- | ------------- |
805
+| string | No       | iOS WKWebView |
806
+
807
+---
808
+
798 809
 ### `useWebKit`
799 810
 
800 811
 If true, use WKWebView instead of UIWebView.

+ 1
- 0
ios/RNCWKWebView.h View File

@@ -49,6 +49,7 @@
49 49
 @property (nonatomic, assign) BOOL showsHorizontalScrollIndicator;
50 50
 @property (nonatomic, assign) BOOL showsVerticalScrollIndicator;
51 51
 @property (nonatomic, assign) BOOL directionalLockEnabled;
52
+@property (nonatomic, copy) NSString *allowingReadAccessToURL;
52 53
 
53 54
 + (void)setClientAuthenticationCredential:(nullable NSURLCredential*)credential;
54 55
 - (void)postMessage:(NSString *)message;

+ 13
- 1
ios/RNCWKWebView.m View File

@@ -377,6 +377,17 @@ static NSURLCredential* clientAuthenticationCredential;
377 377
   }
378 378
 }
379 379
 
380
+- (void)setAllowingReadAccessToURL:(NSString *)allowingReadAccessToURL
381
+{
382
+  if (![_allowingReadAccessToURL isEqualToString:allowingReadAccessToURL]) {
383
+    _allowingReadAccessToURL = [allowingReadAccessToURL copy];
384
+
385
+    if (_webView != nil) {
386
+      [self visitSource];
387
+    }
388
+  }
389
+}
390
+
380 391
 - (void)setContentInset:(UIEdgeInsets)contentInset
381 392
 {
382 393
   _contentInset = contentInset;
@@ -422,7 +433,8 @@ static NSURLCredential* clientAuthenticationCredential;
422 433
         [_webView loadRequest:request];
423 434
     }
424 435
     else {
425
-        [_webView loadFileURL:request.URL allowingReadAccessToURL:request.URL];
436
+        NSURL* readAccessUrl = _allowingReadAccessToURL ? [NSURL URLWithString:_allowingReadAccessToURL] : request.URL;
437
+        [_webView loadFileURL:request.URL allowingReadAccessToURL:readAccessUrl];
426 438
     }
427 439
 }
428 440
 

+ 1
- 0
ios/RNCWKWebViewManager.m View File

@@ -64,6 +64,7 @@ RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
64 64
 RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
65 65
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
66 66
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
67
+RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
67 68
 
68 69
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
69 70
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)

+ 13
- 0
src/WebViewTypes.ts View File

@@ -254,6 +254,7 @@ export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
254 254
 }
255 255
 
256 256
 export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
257
+  allowingReadAccessToURL?: string;
257 258
   allowsBackForwardNavigationGestures?: boolean;
258 259
   allowsInlineMediaPlayback?: boolean;
259 260
   allowsLinkPreview?: boolean;
@@ -432,6 +433,18 @@ export interface IOSWebViewProps extends WebViewSharedProps {
432 433
    * @platform ios
433 434
    */
434 435
   keyboardDisplayRequiresUserAction?: boolean;
436
+
437
+  /**
438
+   * A String value that indicates which URLs the WebView's file can then
439
+   * reference in scripts, AJAX requests, and CSS imports. This is only used
440
+   * in `RNCWKWebView` for WebViews that are loaded with a source.uri set to a
441
+   * `'file://'` URL.
442
+   * 
443
+   * If not provided, the default is to only allow read access to the URL
444
+   * provided in source.uri itself.
445
+   * @platform ios
446
+   */
447
+  allowingReadAccessToURL?: string;
435 448
 }
436 449
 
437 450
 export interface AndroidWebViewProps extends WebViewSharedProps {