|
1 |
| -# @react-native-community/react-native-cookies |
| 1 | +# React Native Cookies - A Cookie Manager for React Native |
2 | 2 | Cookie Manager for React Native
|
| 3 | + |
| 4 | +## Maintainers |
| 5 | +- [Jason Safaiyeh](https://github.com/safaiyeh) ([Twitter @safaiyeh](https://twitter.com/safaiyeh)) from [Sumo Logic](https://www.sumologic.com) |
| 6 | + |
| 7 | +## Platforms Supported |
| 8 | +- [x] iOS |
| 9 | +- [x] Android |
| 10 | + |
| 11 | +Currently lacking support for Windows, Web, and Expo. Support for these platforms will created when there is a need for them. Starts with a posted issue. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +``` |
| 16 | +yarn add @react-native-community/react-native-cookies |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +## Setup (React Native < 0.60.0) |
| 21 | + |
| 22 | +### Automatic (recommended) |
| 23 | + |
| 24 | +``` |
| 25 | +react-native link @react-native-community/react-native-cookies |
| 26 | +``` |
| 27 | + |
| 28 | +### Manual |
| 29 | + |
| 30 | +If automatic linking does not work, you can manually link this library by following the instructions below: |
| 31 | + |
| 32 | +#### iOS |
| 33 | + |
| 34 | +1. Open your project in Xcode, right click on `Libraries` and click `Add |
| 35 | + Files to "Your Project Name"` Look under `node_modules/@react-native-community/react-native-cookies/ios` and add `RNCookieManagerIOS.xcodeproj`. |
| 36 | +2. Add `libRNCookieManagerIOS.a` to `Build Phases -> Link Binary With Libraries. |
| 37 | +3. Clean and rebuild your project |
| 38 | + |
| 39 | +#### Android |
| 40 | + |
| 41 | +Run `react-native link` to link the react-native-cookies library. |
| 42 | + |
| 43 | +Or if you have trouble, make the following additions to the given files manually: |
| 44 | + |
| 45 | +**android/settings.gradle** |
| 46 | + |
| 47 | +```gradle |
| 48 | +include ':@react-native-community_react-native-cookies' |
| 49 | +project(':@react-native-community_react-native-cookies').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/react-native-cookies/android') |
| 50 | +``` |
| 51 | + |
| 52 | +**android/app/build.gradle** |
| 53 | + |
| 54 | +```gradle |
| 55 | +dependencies { |
| 56 | + ... |
| 57 | + implementation project(':@react-native-community_react-native-cookies') |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +**MainApplication.java** |
| 62 | + |
| 63 | +On top, where imports are: |
| 64 | + |
| 65 | +```java |
| 66 | +import com.reactnativecommunity.cookies.CookieManagerPackage; |
| 67 | +``` |
| 68 | + |
| 69 | +Add the `CookieManagerPackage` class to your list of exported packages. |
| 70 | + |
| 71 | +```java |
| 72 | +@Override |
| 73 | +protected List<ReactPackage> getPackages() { |
| 74 | + return Arrays.asList( |
| 75 | + new MainReactPackage(), |
| 76 | + new CookieManagerPackage() |
| 77 | + ); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +## Usage |
| 84 | + |
| 85 | +```javascript |
| 86 | +import CookieManager from 'react-native-cookie-store'; |
| 87 | + |
| 88 | +// set a cookie (IOS ONLY) |
| 89 | +CookieManager.set({ |
| 90 | + name: 'myCookie', |
| 91 | + value: 'myValue', |
| 92 | + domain: 'some domain', |
| 93 | + origin: 'some origin', |
| 94 | + path: '/', |
| 95 | + version: '1', |
| 96 | + expiration: '2015-05-30T12:30:00.00-05:00' |
| 97 | +}).then((res) => { |
| 98 | + console.log('CookieManager.set =>', res); |
| 99 | +}); |
| 100 | + |
| 101 | +// Set cookies from a response header |
| 102 | +// This allows you to put the full string provided by a server's Set-Cookie |
| 103 | +// response header directly into the cookie store. |
| 104 | +CookieManager.setFromResponse( |
| 105 | + 'http://example.com', |
| 106 | + 'user_session=abcdefg; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly') |
| 107 | + .then((res) => { |
| 108 | + // `res` will be true or false depending on success. |
| 109 | + console.log('CookieManager.setFromResponse =>', res); |
| 110 | + }); |
| 111 | + |
| 112 | +// Get cookies as a request header string |
| 113 | +CookieManager.get('http://example.com') |
| 114 | + .then((res) => { |
| 115 | + console.log('CookieManager.get =>', res); // => 'user_session=abcdefg; path=/;' |
| 116 | + }); |
| 117 | + |
| 118 | +// list cookies (IOS ONLY) |
| 119 | +// useWebKit: boolean |
| 120 | +CookieManager.getAll(useWebKit) |
| 121 | + .then((res) => { |
| 122 | + console.log('CookieManager.getAll =>', res); |
| 123 | + }); |
| 124 | + |
| 125 | +// clear cookies |
| 126 | +CookieManager.clearAll() |
| 127 | + .then((res) => { |
| 128 | + console.log('CookieManager.clearAll =>', res); |
| 129 | + }); |
| 130 | + |
| 131 | +// clear a specific cookie by its name (IOS ONLY) |
| 132 | +CookieManager.clearByName('cookie_name') |
| 133 | + .then((res) => { |
| 134 | + console.log('CookieManager.clearByName =>', res); |
| 135 | + }); |
| 136 | + |
| 137 | +``` |
| 138 | + |
| 139 | +### WebKit-Support (iOS only) |
| 140 | +React Native comes with a WebView component, which uses UIWebView on iOS. Introduced in iOS 8 Apple implemented the WebKit-Support with all the performance boost. |
| 141 | + |
| 142 | +To use this it's required to use a special implementation of the WebView component (e.g. [react-native-wkwebview](https://github.com/CRAlpha/react-native-wkwebview)). |
| 143 | + |
| 144 | +This special implementation of the WebView component stores the cookies __not__ in `NSHTTPCookieStorage` anymore. The new cookie-storage is `WKHTTPCookieStore` and implementes a differnt interface. |
| 145 | + |
| 146 | +To use this _CookieManager_ with WebKit-Support we extended the interface with the attribute `useWebKit` (a boolean value, default: `FASLE`) for the following methods: |
| 147 | + |
| 148 | +|Method|WebKit-Support|Method-Signature| |
| 149 | +|---|---|---| |
| 150 | +|getAll| Yes | `CookieManager.getAll(useWebKit:boolean)` | |
| 151 | +|clearAll| Yes | `CookieManager.clearAll(useWebKit:boolean)` | |
| 152 | +|get| Yes | `CookieManager.get(url:string, useWebKit:boolean)` | |
| 153 | +|set| Yes | `CookieManager.set(cookie:object, useWebKit:boolean)` | |
| 154 | + |
| 155 | +##### Usage |
| 156 | +```javascript |
| 157 | +import CookieManager from 'react-native-cookie-store'; |
| 158 | + |
| 159 | +const useWebKit = true; |
| 160 | + |
| 161 | +// list cookies (IOS ONLY) |
| 162 | +CookieManager.getAll(useWebKit) |
| 163 | + .then((res) => { |
| 164 | + console.log('CookieManager.getAll from webkit-view =>', res); |
| 165 | + }); |
| 166 | + |
| 167 | +// clear cookies |
| 168 | +CookieManager.clearAll(useWebKit) |
| 169 | + .then((res) => { |
| 170 | + console.log('CookieManager.clearAll from webkit-view =>', res); |
| 171 | + }); |
| 172 | + |
| 173 | +// Get cookies as a request header string |
| 174 | +CookieManager.get('http://example.com', useWebKit) |
| 175 | + .then((res) => { |
| 176 | + console.log('CookieManager.get from webkit-view =>', res); |
| 177 | + // => 'user_session=abcdefg; path=/;' |
| 178 | + }); |
| 179 | + |
| 180 | +// set a cookie (IOS ONLY) |
| 181 | +const newCookie: = { |
| 182 | + name: 'myCookie', |
| 183 | + value: 'myValue', |
| 184 | + domain: 'some domain', |
| 185 | + origin: 'some origin', |
| 186 | + path: '/', |
| 187 | + version: '1', |
| 188 | + expiration: '2015-05-30T12:30:00.00-05:00' |
| 189 | +}; |
| 190 | + |
| 191 | +CookieManager.set(newCookie, useWebKit) |
| 192 | + .then((res) => { |
| 193 | + console.log('CookieManager.set from webkit-view =>', res); |
| 194 | + }); |
| 195 | +``` |
| 196 | + |
0 commit comments