├── android ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── psykar │ │ └── cookiemanager │ │ ├── CookieManagerPackage.java │ │ └── CookieManagerModule.java └── build.gradle ├── ios ├── RNCookieManagerIOS.xcodeproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── jmrodriguez.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── josephferraro.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ ├── xcuserdata │ │ ├── jmrodriguez.xcuserdatad │ │ │ └── xcschemes │ │ │ │ ├── xcschememanagement.plist │ │ │ │ └── RNCookieManagerIOS.xcscheme │ │ └── josephferraro.xcuserdatad │ │ │ └── xcschemes │ │ │ ├── xcschememanagement.plist │ │ │ └── RNCookieManagerIOS.xcscheme │ └── project.pbxproj ├── RNCookieManagerIOS │ ├── RNCookieManagerIOS.h │ └── RNCookieManagerIOS.m └── react-native-cookies.podspec ├── .gitignore ├── package.json ├── LICENSE.md ├── index.js └── README.md /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/project.xcworkspace/xcuserdata/jmrodriguez.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeferraro/react-native-cookies/HEAD/ios/RNCookieManagerIOS.xcodeproj/project.xcworkspace/xcuserdata/jmrodriguez.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/project.xcworkspace/xcuserdata/josephferraro.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeferraro/react-native-cookies/HEAD/ios/RNCookieManagerIOS.xcodeproj/project.xcworkspace/xcuserdata/josephferraro.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | RNCookieManagerIOS.xcodeproj/project.xcworkspace/xcuserdata/josephferraro.xcuserdatad/UserInterfaceState.xcuserstate 3 | 4 | RNCookieManagerIOS.xcodeproj/xcuserdata/josephferraro.xcuserdatad/xcschemes/RNCookieManagerIOS.xcscheme 5 | 6 | RNCookieManagerIOS.xcodeproj/xcuserdata/josephferraro.xcuserdatad/xcschemes/xcschememanagement.plist 7 | 8 | node_modules/ -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS/RNCookieManagerIOS.h: -------------------------------------------------------------------------------- 1 | // RNCookieManagerIOS.h 2 | #if __has_include("RCTBridgeModule.h") 3 | #import "RCTBridgeModule.h" 4 | #else 5 | #import 6 | #endif 7 | 8 | #import 9 | 10 | @interface RNCookieManagerIOS : NSObject 11 | 12 | @property (nonatomic, strong) NSDateFormatter *formatter; 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-cookies", 3 | "version": "3.3.0", 4 | "description": "Cookie manager for react native", 5 | "keywords": [ 6 | "react", 7 | "react native", 8 | "react-component", 9 | "cookies", 10 | "webview", 11 | "react-native" 12 | ], 13 | "main": "index.js", 14 | "author": "@joeferraro", 15 | "license": "MIT", 16 | "dependencies": { 17 | "invariant": "^2.1.0" 18 | } 19 | } -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/xcuserdata/jmrodriguez.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RNCookieManagerIOS.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 1BD725D91CF77A8B005DBD79 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/xcuserdata/josephferraro.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RNCookieManagerIOS.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 1BD725D91CF77A8B005DBD79 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /ios/react-native-cookies.podspec: -------------------------------------------------------------------------------- 1 | require 'json' 2 | 3 | package = JSON.parse(File.read(File.join(__dir__, '../package.json'))) 4 | 5 | Pod::Spec.new do |s| 6 | s.name = package['name'] 7 | s.version = "3.2.0" 8 | s.summary = package['description'] 9 | s.description = <<-DESC 10 | Cookie Manager for React Native 11 | DESC 12 | s.homepage = "https://github.com/joeferraro/react-native-cookies" 13 | s.license = package['license'] 14 | s.author = "joeferraro" 15 | s.source = { :git => "git@github.com:joeferraro/react-native-cookies.git", :tag => "v#{s.version}" } 16 | s.requires_arc = true 17 | s.platform = :ios, "7.0" 18 | s.preserve_paths = "*.framework" 19 | s.source_files = 'RNCookieManagerIOS/*.{h,m}' 20 | s.dependency 'React' 21 | end 22 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | def safeExtGet(prop, fallback) { 2 | rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback 3 | } 4 | 5 | buildscript { 6 | repositories { 7 | jcenter() 8 | } 9 | 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:2.3.3' 12 | } 13 | } 14 | 15 | apply plugin: 'com.android.library' 16 | 17 | android { 18 | compileSdkVersion safeExtGet('compileSdkVersion', 26) 19 | buildToolsVersion safeExtGet('buildToolsVersion', '26.0.3') 20 | 21 | defaultConfig { 22 | minSdkVersion safeExtGet('minSdkVersion', 16) 23 | targetSdkVersion safeExtGet('targetSdkVersion', 26) 24 | versionCode 1 25 | versionName "1.0" 26 | } 27 | lintOptions { 28 | abortOnError false 29 | } 30 | } 31 | 32 | repositories { 33 | mavenCentral() 34 | } 35 | 36 | dependencies { 37 | compile 'com.facebook.react:react-native:+' 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Joseph P. Ferraro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /android/src/main/java/com/psykar/cookiemanager/CookieManagerPackage.java: -------------------------------------------------------------------------------- 1 | package com.psykar.cookiemanager; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.NativeModule; 5 | import com.facebook.react.bridge.JavaScriptModule; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.uimanager.ViewManager; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Arrays; 11 | import java.util.Collections; 12 | import java.util.List; 13 | 14 | public class CookieManagerPackage implements ReactPackage { 15 | 16 | public CookieManagerPackage() {} 17 | 18 | @Override 19 | public List createNativeModules( 20 | ReactApplicationContext reactContext) { 21 | List modules = new ArrayList<>(); 22 | 23 | modules.add(new CookieManagerModule(reactContext)); 24 | return modules; 25 | } 26 | 27 | // Deprecated 28 | public List> createJSModules() { 29 | return Collections.emptyList(); 30 | } 31 | 32 | @Override 33 | public List createViewManagers(ReactApplicationContext reactContext) { 34 | return Arrays.asList(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { NativeModules, Platform } from 'react-native'; 3 | const invariant = require('invariant'); 4 | const RNCookieManagerIOS = NativeModules.RNCookieManagerIOS; 5 | const RNCookieManagerAndroid = NativeModules.RNCookieManagerAndroid; 6 | 7 | let CookieManager; 8 | 9 | if (Platform.OS === 'ios') { 10 | invariant(RNCookieManagerIOS, 11 | 'react-native-cookies: Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project'); 12 | CookieManager = RNCookieManagerIOS; 13 | } else if (Platform.OS === 'android') { 14 | invariant(RNCookieManagerAndroid, 15 | 'react-native-cookies: Import libraries to android "react-native link react-native-cookies"'); 16 | CookieManager = RNCookieManagerAndroid; 17 | } else { 18 | invariant(CookieManager, 'react-native-cookies: Invalid platform. This library only supports Android and iOS.'); 19 | } 20 | 21 | const functions = [ 22 | 'setFromResponse', 23 | 'getFromResponse', 24 | 'clearByName' 25 | ]; 26 | 27 | module.exports = { 28 | getAll: (useWebKit = false) => CookieManager.getAll(useWebKit), 29 | clearAll: (useWebKit = false) => CookieManager.clearAll(useWebKit), 30 | get: (url, useWebKit = false) => CookieManager.get(url, useWebKit), 31 | set: (cookie, useWebKit = false) => CookieManager.set(cookie, useWebKit), 32 | }; 33 | 34 | for (var i = 0; i < functions.length; i++) { 35 | module.exports[functions[i]] = CookieManager[functions[i]]; 36 | } 37 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/xcuserdata/jmrodriguez.xcuserdatad/xcschemes/RNCookieManagerIOS.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/xcuserdata/josephferraro.xcuserdatad/xcschemes/RNCookieManagerIOS.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /android/src/main/java/com/psykar/cookiemanager/CookieManagerModule.java: -------------------------------------------------------------------------------- 1 | package com.psykar.cookiemanager; 2 | 3 | import com.facebook.react.modules.network.ForwardingCookieHandler; 4 | import com.facebook.react.bridge.ReactApplicationContext; 5 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 6 | import com.facebook.react.bridge.ReactMethod; 7 | import com.facebook.react.bridge.ReadableMap; 8 | import com.facebook.react.bridge.Callback; 9 | import com.facebook.react.bridge.Arguments; 10 | import com.facebook.react.bridge.WritableMap; 11 | import com.facebook.react.bridge.Promise; 12 | 13 | import java.io.IOException; 14 | import java.net.URISyntaxException; 15 | import java.net.URI; 16 | import java.util.Collections; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | public class CookieManagerModule extends ReactContextBaseJavaModule { 22 | 23 | private ForwardingCookieHandler cookieHandler; 24 | 25 | public CookieManagerModule(ReactApplicationContext context) { 26 | super(context); 27 | this.cookieHandler = new ForwardingCookieHandler(context); 28 | } 29 | 30 | public String getName() { 31 | return "RNCookieManagerAndroid"; 32 | } 33 | 34 | @ReactMethod 35 | public void set(ReadableMap cookie, boolean useWebKit, final Promise promise) throws Exception { 36 | throw new Exception("Cannot call on android, try setFromResponse"); 37 | } 38 | 39 | @ReactMethod 40 | public void setFromResponse(String url, String value, final Promise promise) throws URISyntaxException, IOException { 41 | Map headers = new HashMap>(); 42 | // Pretend this is a header 43 | headers.put("Set-Cookie", Collections.singletonList(value)); 44 | URI uri = new URI(url); 45 | try { 46 | this.cookieHandler.put(uri, headers); 47 | promise.resolve(true); 48 | } catch (IOException e) { 49 | promise.resolve(false); 50 | } 51 | } 52 | 53 | @ReactMethod 54 | public void getFromResponse(String url, Promise promise) throws URISyntaxException, IOException { 55 | promise.resolve(url); 56 | } 57 | 58 | @ReactMethod 59 | public void getAll(boolean useWebKit, Promise promise) throws Exception { 60 | throw new Exception("Cannot get all cookies on android, try getCookieHeader(url)"); 61 | } 62 | 63 | @ReactMethod 64 | public void get(String url, boolean useWebKit, Promise promise) throws URISyntaxException, IOException { 65 | URI uri = new URI(url); 66 | 67 | Map> cookieMap = this.cookieHandler.get(uri, new HashMap()); 68 | // If only the variables were public 69 | List cookieList = cookieMap.get("Cookie"); 70 | WritableMap map = Arguments.createMap(); 71 | if (cookieList != null) { 72 | String[] cookies = cookieList.get(0).split(";"); 73 | for (int i = 0; i < cookies.length; i++) { 74 | String[] cookie = cookies[i].split("=", 2); 75 | if (cookie.length > 1) { 76 | map.putString(cookie[0].trim(), cookie[1]); 77 | } 78 | } 79 | } 80 | promise.resolve(map); 81 | } 82 | 83 | @ReactMethod 84 | public void clearAll(boolean useWebKit, final Promise promise) { 85 | this.cookieHandler.clearCookies(new Callback() { 86 | public void invoke(Object... args) { 87 | promise.resolve(null); 88 | } 89 | }); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## react-native-cookies 2 | 3 | Cookie manager for react native. 4 | 5 | [![npm version](https://badge.fury.io/js/react-native-cookies.svg)](https://badge.fury.io/js/react-native-cookies) 6 | [![npm downloads](https://img.shields.io/npm/dm/react-native-cookies.svg)](https://www.npmjs.com/package/react-native-cookies) 7 | [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/joeferraro/react-native-cookies/master/LICENSE.md) 8 | 9 | ## This project has moved 10 | 11 | This project has been moved to the [React Native Community](https://github.com/react-native-community/react-native-cookies). 12 | 13 | ### Installation 14 | 15 | ``` 16 | yarn add react-native-cookies 17 | ``` 18 | 19 | ### Linking 20 | 21 | #### Automatic (recommended) 22 | 23 | ``` 24 | react-native link react-native-cookies 25 | ``` 26 | 27 | #### Manual 28 | 29 | If automatic linking does not work, you can manually link this library by following the instructions below: 30 | 31 | ##### iOS 32 | 33 | 1. Open your project in Xcode, right click on `Libraries` and click `Add 34 | Files to "Your Project Name"` Look under `node_modules/react-native-cookies/ios` and add `RNCookieManagerIOS.xcodeproj`. 35 | 2. Add `libRNCookieManagerIOS.a` to `Build Phases -> Link Binary With Libraries. 36 | 3. Clean and rebuild your project 37 | 38 | ##### Android 39 | 40 | Run `react-native link` to link the react-native-cookies library. 41 | 42 | Or if you have trouble, make the following additions to the given files manually: 43 | 44 | **android/settings.gradle** 45 | 46 | ```gradle 47 | include ':react-native-cookies' 48 | project(':react-native-cookies').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-cookies/android') 49 | ``` 50 | 51 | **android/app/build.gradle** 52 | 53 | ```gradle 54 | dependencies { 55 | ... 56 | compile project(':react-native-cookies') 57 | } 58 | ``` 59 | 60 | **MainApplication.java** 61 | 62 | On top, where imports are: 63 | 64 | ```java 65 | import com.psykar.cookiemanager.CookieManagerPackage; 66 | ``` 67 | 68 | Add the `CookieManagerPackage` class to your list of exported packages. 69 | 70 | ```java 71 | @Override 72 | protected List getPackages() { 73 | return Arrays.asList( 74 | new MainReactPackage(), 75 | new CookieManagerPackage() 76 | ); 77 | } 78 | ``` 79 | 80 | 81 | 82 | ### Usage 83 | 84 | ```javascript 85 | import CookieManager from 'react-native-cookies'; 86 | 87 | // set a cookie (IOS ONLY) 88 | CookieManager.set({ 89 | name: 'myCookie', 90 | value: 'myValue', 91 | domain: 'some domain', 92 | origin: 'some origin', 93 | path: '/', 94 | version: '1', 95 | expiration: '2015-05-30T12:30:00.00-05:00' 96 | }).then((res) => { 97 | console.log('CookieManager.set =>', res); 98 | }); 99 | 100 | // Set cookies from a response header 101 | // This allows you to put the full string provided by a server's Set-Cookie 102 | // response header directly into the cookie store. 103 | CookieManager.setFromResponse( 104 | 'http://example.com', 105 | 'user_session=abcdefg; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly') 106 | .then((res) => { 107 | // `res` will be true or false depending on success. 108 | console.log('CookieManager.setFromResponse =>', res); 109 | }); 110 | 111 | // Get cookies as a request header string 112 | CookieManager.get('http://example.com') 113 | .then((res) => { 114 | console.log('CookieManager.get =>', res); // => 'user_session=abcdefg; path=/;' 115 | }); 116 | 117 | // list cookies (IOS ONLY) 118 | CookieManager.getAll() 119 | .then((res) => { 120 | console.log('CookieManager.getAll =>', res); 121 | }); 122 | 123 | // clear cookies 124 | CookieManager.clearAll() 125 | .then((res) => { 126 | console.log('CookieManager.clearAll =>', res); 127 | }); 128 | 129 | // clear a specific cookie by its name (IOS ONLY) 130 | CookieManager.clearByName('cookie_name') 131 | .then((res) => { 132 | console.log('CookieManager.clearByName =>', res); 133 | }); 134 | 135 | ``` 136 | 137 | #### WebKit-Support (iOS only) 138 | 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. 139 | 140 | 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)). 141 | 142 | 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. 143 | 144 | To use this _CookieManager_ with WebKit-Support we extended the interface with the attribute `useWebKit` (a boolean value, default: `FASLE`) for the following methods: 145 | 146 | |Method|WebKit-Support|Method-Signature| 147 | |---|---|---| 148 | |getAll| Yes | `CookieManager.getAll(useWebKit:boolean)` | 149 | |clearAll| Yes | `CookieManager.clearAll(useWebKit:boolean)` | 150 | |get| Yes | `CookieManager.get(url:string, useWebKit:boolean)` | 151 | |set| Yes | `CookieManager.set(cookie:object, useWebKit:boolean)` | 152 | 153 | ##### Usage 154 | ```javascript 155 | import CookieManager from 'react-native-cookies'; 156 | 157 | const useWebKit = true; 158 | 159 | // list cookies (IOS ONLY) 160 | CookieManager.getAll(useWebKit) 161 | .then((res) => { 162 | console.log('CookieManager.getAll from webkit-view =>', res); 163 | }); 164 | 165 | // clear cookies 166 | CookieManager.clearAll(useWebKit) 167 | .then((res) => { 168 | console.log('CookieManager.clearAll from webkit-view =>', res); 169 | }); 170 | 171 | // Get cookies as a request header string 172 | CookieManager.get('http://example.com', useWebKit) 173 | .then((res) => { 174 | console.log('CookieManager.get from webkit-view =>', res); 175 | // => 'user_session=abcdefg; path=/;' 176 | }); 177 | 178 | // set a cookie (IOS ONLY) 179 | const newCookie: = { 180 | name: 'myCookie', 181 | value: 'myValue', 182 | domain: 'some domain', 183 | origin: 'some origin', 184 | path: '/', 185 | version: '1', 186 | expiration: '2015-05-30T12:30:00.00-05:00' 187 | }; 188 | 189 | CookieManager.set(newCookie, useWebKit) 190 | .then((res) => { 191 | console.log('CookieManager.set from webkit-view =>', res); 192 | }); 193 | ``` 194 | 195 | ### TODO 196 | 197 | - Proper `getAll` dictionary by domain 198 | - Proper error handling 199 | - Anything else? 200 | 201 | PR's welcome! 202 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1BD726021CF77DD1005DBD79 /* RNCookieManagerIOS.m in Sources */ = {isa = PBXBuildFile; fileRef = 1BD726011CF77DD1005DBD79 /* RNCookieManagerIOS.m */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | 1BD725D81CF77A8B005DBD79 /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = "include/$(PRODUCT_NAME)"; 18 | dstSubfolderSpec = 16; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 0; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | 1BD725DA1CF77A8B005DBD79 /* libRNCookieManagerIOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNCookieManagerIOS.a; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | 1BD726001CF77DD1005DBD79 /* RNCookieManagerIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNCookieManagerIOS.h; sourceTree = ""; }; 28 | 1BD726011CF77DD1005DBD79 /* RNCookieManagerIOS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCookieManagerIOS.m; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 1BD725D71CF77A8B005DBD79 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 1BD725CD1CF7795C005DBD79 = { 43 | isa = PBXGroup; 44 | children = ( 45 | 1BD725FF1CF77DD1005DBD79 /* RNCookieManagerIOS */, 46 | 1BD725DB1CF77A8B005DBD79 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | 1BD725DB1CF77A8B005DBD79 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | 1BD725DA1CF77A8B005DBD79 /* libRNCookieManagerIOS.a */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | 1BD725FF1CF77DD1005DBD79 /* RNCookieManagerIOS */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 1BD726001CF77DD1005DBD79 /* RNCookieManagerIOS.h */, 62 | 1BD726011CF77DD1005DBD79 /* RNCookieManagerIOS.m */, 63 | ); 64 | path = RNCookieManagerIOS; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | 1BD725D91CF77A8B005DBD79 /* RNCookieManagerIOS */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = 1BD725E31CF77A8B005DBD79 /* Build configuration list for PBXNativeTarget "RNCookieManagerIOS" */; 73 | buildPhases = ( 74 | 1BD725D61CF77A8B005DBD79 /* Sources */, 75 | 1BD725D71CF77A8B005DBD79 /* Frameworks */, 76 | 1BD725D81CF77A8B005DBD79 /* CopyFiles */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = RNCookieManagerIOS; 83 | productName = RNCookieManagerIOS; 84 | productReference = 1BD725DA1CF77A8B005DBD79 /* libRNCookieManagerIOS.a */; 85 | productType = "com.apple.product-type.library.static"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | 1BD725CE1CF7795C005DBD79 /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastUpgradeCheck = 0730; 94 | TargetAttributes = { 95 | 1BD725D91CF77A8B005DBD79 = { 96 | CreatedOnToolsVersion = 7.3.1; 97 | }; 98 | }; 99 | }; 100 | buildConfigurationList = 1BD725D11CF7795C005DBD79 /* Build configuration list for PBXProject "RNCookieManagerIOS" */; 101 | compatibilityVersion = "Xcode 3.2"; 102 | developmentRegion = English; 103 | hasScannedForEncodings = 0; 104 | knownRegions = ( 105 | en, 106 | ); 107 | mainGroup = 1BD725CD1CF7795C005DBD79; 108 | productRefGroup = 1BD725DB1CF77A8B005DBD79 /* Products */; 109 | projectDirPath = ""; 110 | projectRoot = ""; 111 | targets = ( 112 | 1BD725D91CF77A8B005DBD79 /* RNCookieManagerIOS */, 113 | ); 114 | }; 115 | /* End PBXProject section */ 116 | 117 | /* Begin PBXSourcesBuildPhase section */ 118 | 1BD725D61CF77A8B005DBD79 /* Sources */ = { 119 | isa = PBXSourcesBuildPhase; 120 | buildActionMask = 2147483647; 121 | files = ( 122 | 1BD726021CF77DD1005DBD79 /* RNCookieManagerIOS.m in Sources */, 123 | ); 124 | runOnlyForDeploymentPostprocessing = 0; 125 | }; 126 | /* End PBXSourcesBuildPhase section */ 127 | 128 | /* Begin XCBuildConfiguration section */ 129 | 1BD725D21CF7795C005DBD79 /* Debug */ = { 130 | isa = XCBuildConfiguration; 131 | buildSettings = { 132 | }; 133 | name = Debug; 134 | }; 135 | 1BD725D31CF7795C005DBD79 /* Release */ = { 136 | isa = XCBuildConfiguration; 137 | buildSettings = { 138 | }; 139 | name = Release; 140 | }; 141 | 1BD725E11CF77A8B005DBD79 /* Debug */ = { 142 | isa = XCBuildConfiguration; 143 | buildSettings = { 144 | ALWAYS_SEARCH_USER_PATHS = NO; 145 | CLANG_ANALYZER_NONNULL = YES; 146 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 147 | CLANG_CXX_LIBRARY = "libc++"; 148 | CLANG_ENABLE_MODULES = YES; 149 | CLANG_ENABLE_OBJC_ARC = YES; 150 | CLANG_WARN_BOOL_CONVERSION = YES; 151 | CLANG_WARN_CONSTANT_CONVERSION = YES; 152 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 153 | CLANG_WARN_EMPTY_BODY = YES; 154 | CLANG_WARN_ENUM_CONVERSION = YES; 155 | CLANG_WARN_INT_CONVERSION = YES; 156 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 157 | CLANG_WARN_UNREACHABLE_CODE = YES; 158 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 159 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 160 | COPY_PHASE_STRIP = NO; 161 | DEBUG_INFORMATION_FORMAT = dwarf; 162 | ENABLE_STRICT_OBJC_MSGSEND = YES; 163 | ENABLE_TESTABILITY = YES; 164 | FRAMEWORK_SEARCH_PATHS = ( 165 | "$(SRCROOT)/../../React/**", 166 | "$(inherited)", 167 | "$(SRCROOT)/node_modules/react-native/React/**", 168 | "$(SRCROOT)/../react-native/React/**", 169 | "$(SRCROOT)/../../../node_modules/react-native/React/**", 170 | ); 171 | GCC_C_LANGUAGE_STANDARD = gnu99; 172 | GCC_DYNAMIC_NO_PIC = NO; 173 | GCC_NO_COMMON_BLOCKS = YES; 174 | GCC_OPTIMIZATION_LEVEL = 0; 175 | GCC_PREPROCESSOR_DEFINITIONS = ( 176 | "DEBUG=1", 177 | "$(inherited)", 178 | ); 179 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 180 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 181 | GCC_WARN_UNDECLARED_SELECTOR = YES; 182 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 183 | GCC_WARN_UNUSED_FUNCTION = YES; 184 | GCC_WARN_UNUSED_VARIABLE = YES; 185 | HEADER_SEARCH_PATHS = ( 186 | "$(SRCROOT)/../../React/**", 187 | "$(inherited)", 188 | "$(SRCROOT)/node_modules/react-native/React/**", 189 | "$(SRCROOT)/../react-native/React/**", 190 | "$(SRCROOT)/../../../node_modules/react-native/React/**", 191 | ); 192 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 193 | MTL_ENABLE_DEBUG_INFO = YES; 194 | ONLY_ACTIVE_ARCH = YES; 195 | OTHER_LDFLAGS = "-ObjC"; 196 | PRODUCT_NAME = "$(TARGET_NAME)"; 197 | SDKROOT = iphoneos; 198 | SKIP_INSTALL = YES; 199 | }; 200 | name = Debug; 201 | }; 202 | 1BD725E21CF77A8B005DBD79 /* Release */ = { 203 | isa = XCBuildConfiguration; 204 | buildSettings = { 205 | ALWAYS_SEARCH_USER_PATHS = NO; 206 | CLANG_ANALYZER_NONNULL = YES; 207 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 208 | CLANG_CXX_LIBRARY = "libc++"; 209 | CLANG_ENABLE_MODULES = YES; 210 | CLANG_ENABLE_OBJC_ARC = YES; 211 | CLANG_WARN_BOOL_CONVERSION = YES; 212 | CLANG_WARN_CONSTANT_CONVERSION = YES; 213 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 214 | CLANG_WARN_EMPTY_BODY = YES; 215 | CLANG_WARN_ENUM_CONVERSION = YES; 216 | CLANG_WARN_INT_CONVERSION = YES; 217 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 218 | CLANG_WARN_UNREACHABLE_CODE = YES; 219 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 220 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 221 | COPY_PHASE_STRIP = NO; 222 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 223 | ENABLE_NS_ASSERTIONS = NO; 224 | ENABLE_STRICT_OBJC_MSGSEND = YES; 225 | FRAMEWORK_SEARCH_PATHS = ( 226 | "$(SRCROOT)/../../React/**", 227 | "$(inherited)", 228 | "$(SRCROOT)/node_modules/react-native/React/**", 229 | "$(SRCROOT)/../react-native/React/**", 230 | "$(SRCROOT)/../../../node_modules/react-native/React/**", 231 | ); 232 | GCC_C_LANGUAGE_STANDARD = gnu99; 233 | GCC_NO_COMMON_BLOCKS = YES; 234 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 235 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 236 | GCC_WARN_UNDECLARED_SELECTOR = YES; 237 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 238 | GCC_WARN_UNUSED_FUNCTION = YES; 239 | GCC_WARN_UNUSED_VARIABLE = YES; 240 | HEADER_SEARCH_PATHS = ( 241 | "$(SRCROOT)/../../React/**", 242 | "$(inherited)", 243 | "$(SRCROOT)/node_modules/react-native/React/**", 244 | "$(SRCROOT)/../react-native/React/**", 245 | "$(SRCROOT)/../../../node_modules/react-native/React/**", 246 | ); 247 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 248 | MTL_ENABLE_DEBUG_INFO = NO; 249 | OTHER_LDFLAGS = "-ObjC"; 250 | PRODUCT_NAME = "$(TARGET_NAME)"; 251 | SDKROOT = iphoneos; 252 | SKIP_INSTALL = YES; 253 | VALIDATE_PRODUCT = YES; 254 | }; 255 | name = Release; 256 | }; 257 | /* End XCBuildConfiguration section */ 258 | 259 | /* Begin XCConfigurationList section */ 260 | 1BD725D11CF7795C005DBD79 /* Build configuration list for PBXProject "RNCookieManagerIOS" */ = { 261 | isa = XCConfigurationList; 262 | buildConfigurations = ( 263 | 1BD725D21CF7795C005DBD79 /* Debug */, 264 | 1BD725D31CF7795C005DBD79 /* Release */, 265 | ); 266 | defaultConfigurationIsVisible = 0; 267 | defaultConfigurationName = Release; 268 | }; 269 | 1BD725E31CF77A8B005DBD79 /* Build configuration list for PBXNativeTarget "RNCookieManagerIOS" */ = { 270 | isa = XCConfigurationList; 271 | buildConfigurations = ( 272 | 1BD725E11CF77A8B005DBD79 /* Debug */, 273 | 1BD725E21CF77A8B005DBD79 /* Release */, 274 | ); 275 | defaultConfigurationIsVisible = 0; 276 | }; 277 | /* End XCConfigurationList section */ 278 | }; 279 | rootObject = 1BD725CE1CF7795C005DBD79 /* Project object */; 280 | } 281 | -------------------------------------------------------------------------------- /ios/RNCookieManagerIOS/RNCookieManagerIOS.m: -------------------------------------------------------------------------------- 1 | #import "RNCookieManagerIOS.h" 2 | #if __has_include("RCTConvert.h") 3 | #import "RCTConvert.h" 4 | #else 5 | #import 6 | #endif 7 | 8 | static NSString * const NOT_AVAILABLE_ERROR_MESSAGE = @"WebKit/WebKit-Components are only available with iOS11 and higher!"; 9 | 10 | @implementation RNCookieManagerIOS 11 | 12 | - (instancetype)init 13 | { 14 | self = [super init]; 15 | if (self) { 16 | self.formatter = [NSDateFormatter new]; 17 | [self.formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"]; 18 | } 19 | return self; 20 | } 21 | 22 | + (BOOL)requiresMainQueueSetup 23 | { 24 | return NO; 25 | } 26 | 27 | RCT_EXPORT_MODULE() 28 | 29 | RCT_EXPORT_METHOD( 30 | set:(NSDictionary *)props 31 | useWebKit:(BOOL)useWebKit 32 | resolver:(RCTPromiseResolveBlock)resolve 33 | rejecter:(RCTPromiseRejectBlock)reject) 34 | { 35 | NSString *name = [RCTConvert NSString:props[@"name"]]; 36 | NSString *value = [RCTConvert NSString:props[@"value"]]; 37 | NSString *domain = [RCTConvert NSString:props[@"domain"]]; 38 | NSString *origin = [RCTConvert NSString:props[@"origin"]]; 39 | NSString *path = [RCTConvert NSString:props[@"path"]]; 40 | NSString *version = [RCTConvert NSString:props[@"version"]]; 41 | NSDate *expiration = [RCTConvert NSDate:props[@"expiration"]]; 42 | 43 | NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary]; 44 | [cookieProperties setObject:name forKey:NSHTTPCookieName]; 45 | [cookieProperties setObject:value forKey:NSHTTPCookieValue]; 46 | [cookieProperties setObject:domain forKey:NSHTTPCookieDomain]; 47 | [cookieProperties setObject:origin forKey:NSHTTPCookieOriginURL]; 48 | [cookieProperties setObject:path forKey:NSHTTPCookiePath]; 49 | [cookieProperties setObject:version forKey:NSHTTPCookieVersion]; 50 | [cookieProperties setObject:expiration forKey:NSHTTPCookieExpires]; 51 | 52 | NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties]; 53 | 54 | NSLog(@"SETTING COOKIE"); 55 | NSLog(@"%@", cookie); 56 | 57 | if (useWebKit) { 58 | if (@available(iOS 11.0, *)) { 59 | dispatch_async(dispatch_get_main_queue(), ^(){ 60 | WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; 61 | [cookieStore setCookie:cookie completionHandler:nil]; 62 | resolve(nil); 63 | }); 64 | } else { 65 | reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil); 66 | } 67 | } else { 68 | [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; 69 | resolve(nil); 70 | } 71 | } 72 | 73 | RCT_EXPORT_METHOD(setFromResponse:(NSURL *)url 74 | value:(NSString *)value 75 | resolver:(RCTPromiseResolveBlock)resolve 76 | rejecter:(RCTPromiseRejectBlock)reject) { 77 | NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:@{@"Set-Cookie": value} forURL:url]; 78 | [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:NULL]; 79 | resolve(nil); 80 | } 81 | 82 | RCT_EXPORT_METHOD(getFromResponse:(NSURL *)url 83 | resolver:(RCTPromiseResolveBlock)resolve 84 | rejecter:(RCTPromiseRejectBlock)reject) { 85 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; 86 | [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] 87 | completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 88 | 89 | NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 90 | NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:response.URL]; 91 | NSMutableDictionary *dics = [NSMutableDictionary dictionary]; 92 | 93 | for (int i = 0; i < cookies.count; i++) { 94 | NSHTTPCookie *cookie = [cookies objectAtIndex:i]; 95 | [dics setObject:cookie.value forKey:cookie.name]; 96 | NSLog(@"cookie: name=%@, value=%@", cookie.name, cookie.value); 97 | [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; 98 | } 99 | resolve(dics); 100 | }]; 101 | } 102 | 103 | -(NSString *)getDomainName:(NSURL *) url 104 | { 105 | NSString *separator = @"."; 106 | NSInteger maxLength = 2; 107 | 108 | NSURLComponents *components = [[NSURLComponents alloc]initWithURL:url resolvingAgainstBaseURL:FALSE]; 109 | NSArray *separatedHost = [components.host componentsSeparatedByString:separator]; 110 | NSInteger count = [separatedHost count]; 111 | NSInteger endPosition = count; 112 | NSInteger startPosition = count - maxLength; 113 | 114 | NSMutableString *result = [[NSMutableString alloc]init]; 115 | for (NSUInteger i = startPosition; i != endPosition; i++) { 116 | [result appendString:separator]; 117 | [result appendString:[separatedHost objectAtIndex:i]]; 118 | } 119 | return result; 120 | } 121 | 122 | RCT_EXPORT_METHOD( 123 | get:(NSURL *) url 124 | useWebKit:(BOOL)useWebKit 125 | resolver:(RCTPromiseResolveBlock)resolve 126 | rejecter:(RCTPromiseRejectBlock)reject) 127 | { 128 | if (useWebKit) { 129 | if (@available(iOS 11.0, *)) { 130 | dispatch_async(dispatch_get_main_queue(), ^(){ 131 | NSString *topLevelDomain = [self getDomainName:url]; 132 | 133 | WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; 134 | [cookieStore getAllCookies:^(NSArray *allCookies) { 135 | NSMutableDictionary *cookies = [NSMutableDictionary dictionary]; 136 | for(NSHTTPCookie *currentCookie in allCookies) { 137 | if([currentCookie.domain containsString:topLevelDomain]) { 138 | [cookies setObject:currentCookie.value forKey:currentCookie.name]; 139 | } 140 | } 141 | resolve(cookies); 142 | }]; 143 | }); 144 | } else { 145 | reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil); 146 | } 147 | } else { 148 | NSMutableDictionary *cookies = [NSMutableDictionary dictionary]; 149 | for (NSHTTPCookie *c in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]) { 150 | NSMutableDictionary *d = [NSMutableDictionary dictionary]; 151 | [d setObject:c.value forKey:@"value"]; 152 | [d setObject:c.name forKey:@"name"]; 153 | [d setObject:c.domain forKey:@"domain"]; 154 | [d setObject:c.path forKey:@"path"]; 155 | [d setObject:[self.formatter stringFromDate:c.expiresDate] forKey:@"expiresDate"]; 156 | [cookies setObject:d forKey:c.name]; 157 | } 158 | resolve(cookies); 159 | } 160 | } 161 | 162 | RCT_EXPORT_METHOD( 163 | clearAll:(BOOL)useWebKit 164 | resolver:(RCTPromiseResolveBlock)resolve 165 | rejecter:(RCTPromiseRejectBlock)reject) 166 | { 167 | if (useWebKit) { 168 | if (@available(iOS 11.0, *)) { 169 | dispatch_async(dispatch_get_main_queue(), ^(){ 170 | WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; 171 | [cookieStore getAllCookies:^(NSArray *allCookies) { 172 | for(NSHTTPCookie *currentCookie in allCookies) { 173 | // Uses the NSHTTPCookie directly has no effect, nor deleted the cookie nor thrown an error. 174 | // Create a new cookie with the given values and delete this one do the work. 175 | NSMutableDictionary *cookieData = [NSMutableDictionary dictionary]; 176 | [cookieData setValue:currentCookie.name forKey:NSHTTPCookieName]; 177 | [cookieData setValue:currentCookie.value forKey:NSHTTPCookieValue]; 178 | [cookieData setValue:currentCookie.domain forKey:NSHTTPCookieDomain]; 179 | [cookieData setValue:currentCookie.path forKey:NSHTTPCookiePath]; 180 | 181 | NSHTTPCookie *newCookie = [NSHTTPCookie cookieWithProperties:cookieData]; 182 | [cookieStore deleteCookie:newCookie completionHandler:^{}]; 183 | } 184 | resolve(nil); 185 | }]; 186 | }); 187 | } else { 188 | reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil); 189 | } 190 | } else { 191 | NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 192 | for (NSHTTPCookie *c in cookieStorage.cookies) { 193 | [cookieStorage deleteCookie:c]; 194 | } 195 | resolve(nil); 196 | } 197 | } 198 | 199 | RCT_EXPORT_METHOD(clearByName:(NSString *) name 200 | resolver:(RCTPromiseResolveBlock)resolve 201 | rejecter:(RCTPromiseRejectBlock)reject) { 202 | NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 203 | for (NSHTTPCookie *c in cookieStorage.cookies) { 204 | if ([[c name] isEqualToString:name]) { 205 | [cookieStorage deleteCookie:c]; 206 | } 207 | } 208 | resolve(nil); 209 | } 210 | 211 | RCT_EXPORT_METHOD( 212 | getAll:(BOOL)useWebKit 213 | resolver:(RCTPromiseResolveBlock)resolve 214 | rejecter:(RCTPromiseRejectBlock)reject) 215 | { 216 | if (useWebKit) { 217 | if (@available(iOS 11.0, *)) { 218 | dispatch_async(dispatch_get_main_queue(), ^(){ 219 | WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; 220 | [cookieStore getAllCookies:^(NSArray *allCookies) { 221 | resolve([self createCookieList: allCookies]); 222 | }]; 223 | }); 224 | } else { 225 | reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil); 226 | } 227 | } else { 228 | NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 229 | resolve([self createCookieList:cookieStorage.cookies]); 230 | } 231 | } 232 | 233 | RCT_EXPORT_METHOD(getAll:(RCTPromiseResolveBlock)resolve 234 | rejecter:(RCTPromiseRejectBlock)reject) { 235 | NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 236 | NSMutableDictionary *cookies = [NSMutableDictionary dictionary]; 237 | for (NSHTTPCookie *c in cookieStorage.cookies) { 238 | NSMutableDictionary *d = [NSMutableDictionary dictionary]; 239 | [d setObject:c.value forKey:@"value"]; 240 | [d setObject:c.name forKey:@"name"]; 241 | [d setObject:c.domain forKey:@"domain"]; 242 | [d setObject:c.path forKey:@"path"]; 243 | [d setObject:[self.formatter stringFromDate:c.expiresDate] forKey:@"expiresDate"]; 244 | [cookies setObject:d forKey:c.name]; 245 | } 246 | } 247 | 248 | -(NSDictionary *)createCookieList:(NSArray*)cookies 249 | { 250 | NSMutableDictionary *cookieList = [NSMutableDictionary dictionary]; 251 | for (NSHTTPCookie *cookie in cookies) { 252 | // NSLog(@"COOKIE: %@", cookie); 253 | [cookieList setObject:[self createCookieData:cookie] forKey:cookie.name]; 254 | } 255 | return cookieList; 256 | } 257 | 258 | -(NSDictionary *)createCookieData:(NSHTTPCookie *)cookie 259 | { 260 | NSMutableDictionary *cookieData = [NSMutableDictionary dictionary]; 261 | [cookieData setObject:cookie.value forKey:@"value"]; 262 | [cookieData setObject:cookie.name forKey:@"name"]; 263 | [cookieData setObject:cookie.domain forKey:@"domain"]; 264 | [cookieData setObject:cookie.path forKey:@"path"]; 265 | return cookieData; 266 | } 267 | 268 | @end 269 | --------------------------------------------------------------------------------