Преглед изворни кода

chore(docs): Added guide for working with custom headers and cookies (#994)

* Added guide for working with custom headers and cookies

* Remove extraneous files
Jamon Holmgren пре 5 година
родитељ
комит
8c9f986df0
2 измењених фајлова са 100 додато и 10 уклоњено
  1. 89
    0
      docs/Guide.md
  2. 11
    10
      docs/Reference.md

+ 89
- 0
docs/Guide.md Прегледај датотеку

@@ -389,3 +389,92 @@ export default class App extends Component {
389 389
 This code will result in this alert:
390 390
 
391 391
 <img alt="Alert showing communication from web page to React Native" width="200" src="https://user-images.githubusercontent.com/1479215/53671269-7e822300-3c32-11e9-9937-7ddc34ba8af3.png" />
392
+
393
+### Working with custom headers, sessions, and cookies
394
+
395
+#### Setting Custom Headers
396
+
397
+In React Native WebView, you can set a custom header like this:
398
+
399
+```jsx
400
+<WebView
401
+  source={{
402
+    uri: 'http://example.com',
403
+    headers: {
404
+      'my-custom-header-key': 'my-custom-header-value',
405
+    },
406
+  }}
407
+/>
408
+```
409
+
410
+This will set the header on the first load, but not on subsequent page navigations.
411
+
412
+In order to work around this, you can track the current URL, intercept new page loads, and navigate to them yourself ([original credit for this technique to Chirag Shah from Big Binary](https://blog.bigbinary.com/2016/07/26/passing-request-headers-on-each-webview-request-in-react-native.html)):
413
+
414
+```jsx
415
+const CustomHeaderWebView = props => {
416
+  const { uri, onLoadStart, ...restProps } = props;
417
+  const [currentURI, setURI] = useState(props.source.uri);
418
+  const newSource = { ...props.source, uri: currentURI };
419
+
420
+  return (
421
+    <WebView
422
+      {...restProps}
423
+      source={newSource}
424
+      onShouldStartLoadWithRequest={request => {
425
+        // If we're loading the current URI, allow it to load
426
+        if (request.url === currentURI) return true;
427
+        // We're loading a new URL -- change state first
428
+        setURI(request.url);
429
+        return false;
430
+      }}
431
+    />
432
+  );
433
+};
434
+
435
+<CustomHeaderWebView
436
+  source={{
437
+    uri: 'http://example.com',
438
+    headers: {
439
+      'my-custom-header-key': 'my-custom-header-value',
440
+    },
441
+  }}
442
+/>;
443
+```
444
+
445
+#### Managing Cookies
446
+
447
+You can set cookies on the React Native side using the [react-native-cookies](https://github.com/joeferraro/react-native-cookies) package.
448
+
449
+When you do, you'll likely want to enable the [sharedCookiesEnabled](Reference#sharedCookiesEnabled) prop as well.
450
+
451
+```jsx
452
+const App = () => {
453
+  return (
454
+    <WebView
455
+      source={{ uri: 'http://example.com' }}
456
+      sharedCookiesEnabled={true}
457
+    />
458
+  );
459
+};
460
+```
461
+
462
+If you'd like to send custom cookies in the WebView itself, you can do so in a custom header, like this:
463
+
464
+```jsx
465
+const App = () => {
466
+  return (
467
+    <WebView
468
+      source={{
469
+        uri: 'http://example.com',
470
+        headers: {
471
+          Cookie: 'cookie1=asdf; cookie2=dfasdfdas',
472
+        },
473
+      }}
474
+      sharedCookiesEnabled={true}
475
+    />
476
+  );
477
+};
478
+```
479
+
480
+Note that these cookies will only be sent on the first request unless you use the technique above for [setting custom headers on each page load](#Setting-Custom-Headers).

+ 11
- 10
docs/Reference.md Прегледај датотеку

@@ -85,9 +85,9 @@ The object passed to `source` can have either of the following shapes:
85 85
 
86 86
 **Load uri**
87 87
 
88
-- `uri` (string) - The URI to load in the `WebView`. Can be a local or remote file.
88
+- `uri` (string) - The URI to load in the `WebView`. Can be a local or remote file, and can be changed with React state or props to navigate to a new page.
89 89
 - `method` (string) - The HTTP Method to use. Defaults to GET if not specified. On Android, the only supported methods are GET and POST.
90
-- `headers` (object) - Additional HTTP headers to send with the request. On Android, this can only be used with GET requests.
90
+- `headers` (object) - Additional HTTP headers to send with the request. On Android, this can only be used with GET requests. See the [Guide](Guide.md#setting-custom-headers) for more information on setting custom headers.
91 91
 - `body` (string) - The HTTP body to send with the request. This must be a valid UTF-8 string, and will be sent exactly as specified, with no additional encoding (e.g. URL-escaping or base64) applied. On Android, this can only be used with POST requests.
92 92
 
93 93
 **Static HTML**
@@ -689,7 +689,7 @@ Possible values for `mixedContentMode` are:
689 689
 
690 690
 ### `thirdPartyCookiesEnabled`
691 691
 
692
-Boolean value to enable third party cookies in the `WebView`. Used on Android Lollipop and above only as third party cookies are enabled by default on Android Kitkat and below and on iOS. The default value is `true`.
692
+Boolean value to enable third party cookies in the `WebView`. Used on Android Lollipop and above only as third party cookies are enabled by default on Android Kitkat and below and on iOS. The default value is `true`. For more on cookies, read the [Guide](Guide.md#Managing-Cookies)
693 693
 
694 694
 | Type | Required | Platform |
695 695
 | ---- | -------- | -------- |
@@ -879,7 +879,7 @@ Set whether Geolocation is enabled in the `WebView`. The default value is `false
879 879
 
880 880
 ### `allowFileAccessFromFileURLs`
881 881
 
882
- Boolean that sets whether JavaScript running in the context of a file scheme URL should be allowed to access content from other file scheme URLs. The default value is `false`.
882
+Boolean that sets whether JavaScript running in the context of a file scheme URL should be allowed to access content from other file scheme URLs. The default value is `false`.
883 883
 
884 884
 | Type | Required | Platform |
885 885
 | ---- | -------- | -------- |
@@ -1002,14 +1002,15 @@ Sets whether WebView should use browser caching.
1002 1002
 Overrides the way the cache is used. The way the cache is used is based on the navigation type. For a normal page load, the cache is checked and content is re-validated as needed. When navigating back, content is not revalidated, instead the content is just retrieved from the cache. This property allows the client to override this behavior.
1003 1003
 
1004 1004
 Possible values are:
1005
+
1005 1006
 - `LOAD_DEFAULT` - Default cache usage mode. If the navigation type doesn't impose any specific behavior, use cached resources when they are available and not expired, otherwise load resources from the network.
1006 1007
 - `LOAD_CACHE_ELSE_NETWORK` - Use cached resources when they are available, even if they have expired. Otherwise load resources from the network.
1007 1008
 - `LOAD_NO_CACHE` - Don't use the cache, load from the network.
1008
-- `LOAD_CACHE_ONLY` - Don't use the network, load from the cache. 
1009
- 
1010
-| Type    | Required | Default      | Platform |
1011
-| ------- | -------- | -------------| -------- |
1012
-| string  | No       | LOAD_DEFAULT | Android  |
1009
+- `LOAD_CACHE_ONLY` - Don't use the network, load from the cache.
1010
+
1011
+| Type   | Required | Default      | Platform |
1012
+| ------ | -------- | ------------ | -------- |
1013
+| string | No       | LOAD_DEFAULT | Android  |
1013 1014
 
1014 1015
 ---
1015 1016
 
@@ -1035,7 +1036,7 @@ A Boolean value that determines whether pressing on a link displays a preview of
1035 1036
 
1036 1037
 ### `sharedCookiesEnabled`
1037 1038
 
1038
-Set `true` if shared cookies from `[NSHTTPCookieStorage sharedHTTPCookieStorage]` should used for every load request in the WebView. The default value is `false`.
1039
+Set `true` if shared cookies from `[NSHTTPCookieStorage sharedHTTPCookieStorage]` should used for every load request in the WebView. The default value is `false`. For more on cookies, read the [Guide](Guide.md#Managing-Cookies)
1039 1040
 
1040 1041
 | Type    | Required | Platform |
1041 1042
 | ------- | -------- | -------- |