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
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
42
 - [`directionalLockEnabled`](Reference.md#directionalLockEnabled)
43
 - [`geolocationEnabled`](Reference.md#geolocationenabled)
43
 - [`geolocationEnabled`](Reference.md#geolocationenabled)
44
 - [`allowUniversalAccessFromFileURLs`](Reference.md#allowUniversalAccessFromFileURLs)
44
 - [`allowUniversalAccessFromFileURLs`](Reference.md#allowUniversalAccessFromFileURLs)
45
+- [`allowingReadAccessToURL`](Reference.md#allowingReadAccessToURL)
45
 - [`useWebKit`](Reference.md#usewebkit)
46
 - [`useWebKit`](Reference.md#usewebkit)
46
 - [`url`](Reference.md#url)
47
 - [`url`](Reference.md#url)
47
 - [`html`](Reference.md#html)
48
 - [`html`](Reference.md#html)
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
 ### `useWebKit`
809
 ### `useWebKit`
799
 
810
 
800
 If true, use WKWebView instead of UIWebView.
811
 If true, use WKWebView instead of UIWebView.

+ 1
- 0
ios/RNCWKWebView.h View File

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

+ 13
- 1
ios/RNCWKWebView.m View File

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
 - (void)setContentInset:(UIEdgeInsets)contentInset
391
 - (void)setContentInset:(UIEdgeInsets)contentInset
381
 {
392
 {
382
   _contentInset = contentInset;
393
   _contentInset = contentInset;
422
         [_webView loadRequest:request];
433
         [_webView loadRequest:request];
423
     }
434
     }
424
     else {
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
 RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
64
 RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
65
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
65
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
66
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
66
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
67
+RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
67
 
68
 
68
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
69
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
69
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
70
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)

+ 13
- 0
src/WebViewTypes.ts View File

254
 }
254
 }
255
 
255
 
256
 export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
256
 export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
257
+  allowingReadAccessToURL?: string;
257
   allowsBackForwardNavigationGestures?: boolean;
258
   allowsBackForwardNavigationGestures?: boolean;
258
   allowsInlineMediaPlayback?: boolean;
259
   allowsInlineMediaPlayback?: boolean;
259
   allowsLinkPreview?: boolean;
260
   allowsLinkPreview?: boolean;
432
    * @platform ios
433
    * @platform ios
433
    */
434
    */
434
   keyboardDisplayRequiresUserAction?: boolean;
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
 export interface AndroidWebViewProps extends WebViewSharedProps {
450
 export interface AndroidWebViewProps extends WebViewSharedProps {