Support RCTNetworking#clearCookies on iOS

Summary:
This adds cookie clearing support for iOS to match the existing support on Android.  Helpful for resetting the app to a clean state (say, when logging a user out).
Closes https://github.com/facebook/react-native/pull/9264

Differential Revision: D3776492

Pulled By: javache

fbshipit-source-id: 59ae19ac09d3cf0d0e229cd9e8e30865e65ca96c
This commit is contained in:
Ian MacLeod 2016-08-26 05:34:52 -07:00 committed by Facebook Github Bot 5
parent 1eddd01063
commit 63931b1a68
3 changed files with 21 additions and 1 deletions

View File

@ -41,6 +41,7 @@ var {
var XHRExampleHeaders = require('./XHRExampleHeaders'); var XHRExampleHeaders = require('./XHRExampleHeaders');
var XHRExampleFetch = require('./XHRExampleFetch'); var XHRExampleFetch = require('./XHRExampleFetch');
var XHRExampleOnTimeOut = require('./XHRExampleOnTimeOut'); var XHRExampleOnTimeOut = require('./XHRExampleOnTimeOut');
var XHRExampleCookies = require('./XHRExampleCookies');
/** /**
* Convert number of bytes to MB and round to the nearest 0.1 MB. * Convert number of bytes to MB and round to the nearest 0.1 MB.
@ -432,6 +433,11 @@ exports.examples = [{
render() { render() {
return <XHRExampleOnTimeOut/>; return <XHRExampleOnTimeOut/>;
} }
}, {
title: 'Cookies',
render() {
return <XHRExampleCookies />;
}
}]; }];
var styles = StyleSheet.create({ var styles = StyleSheet.create({

View File

@ -52,7 +52,7 @@ class RCTNetworking extends NativeEventEmitter {
} }
clearCookies(callback: (result: boolean) => any) { clearCookies(callback: (result: boolean) => any) {
console.warn('RCTNetworking.clearCookies is not supported on iOS'); RCTNetworkingNative.clearCookies(callback);
} }
} }

View File

@ -504,6 +504,20 @@ RCT_EXPORT_METHOD(abortRequest:(nonnull NSNumber *)requestID)
[_tasksByRequestID removeObjectForKey:requestID]; [_tasksByRequestID removeObjectForKey:requestID];
} }
RCT_EXPORT_METHOD(clearCookies:(RCTResponseSenderBlock)responseSender)
{
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
if (!storage.cookies.count) {
responseSender(@[@NO]);
return;
}
for (NSHTTPCookie *cookie in storage.cookies) {
[storage deleteCookie:cookie];
}
responseSender(@[@YES]);
}
@end @end
@implementation RCTBridge (RCTNetworking) @implementation RCTBridge (RCTNetworking)