View Full Version : NSHTTPCookieAcceptPolicy

Apr 9, 2009, 04:57 AM
I am building/updating a cookie manager, and would like to bind Safari's cookie policy (Never/Always/Only from sites I visit) and an extra(only from whitelisted sites) to my own implementation with the same values.

Is this possible?

I have found NSHTTPCookieAcceptPolicy, and thought that would be a good place to start, but are a little lost.

does anyone have any ideas, how to acheive what I want.

thanks in advance.

Apr 9, 2009, 05:14 PM
I have added a notification, using notification centre to: NSHTTPCookieManagerAcceptPolicyChangedNotification
using the following code:
NSNotificationCenter * ncpolicy = [NSNotificationCenter defaultCenter];
[ncpolicy addObserver:self selector:@selector(_cookiePolicyChangedNotification:)
name:NSHTTPCookieManagerAcceptPolicyChangedNotification object:nil];

but when I change the policy in Safari, I dont receive any notifications?

Has this changed in Safari 4?

Apr 9, 2009, 05:57 PM
It might be a distributed notification. Try using NSDistributedNotificationCenter instead.

Apr 9, 2009, 06:01 PM
It might be a distributed notification. Try using NSDistributedNotificationCenter instead.


thanks for your help!!

Apr 9, 2009, 09:24 PM

so ive got syncing happening oneway..... when I change the behaviour in Safari, this is reflected in my App.

but not the other way?

I am using the following function:

- (IBAction) changeCookiePolicyButton:(id)sender
[[SCPreferences userDefaults] synchronize];
int policyKey;
policyKey = [[SCPreferences userDefaults] integerForKey:SCPreferencesCookieAcceptPolicy];
if (policyKey == 0)
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
if (policyKey == 1)
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
if (policyKey == 2)
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain];

any ideas?

Apr 10, 2009, 11:31 AM
I would be surprised if Safari actually changes its preferences when other apps change that value.

Edit: well according to the docs it looks like it should. Maybe Safari is special...

Apr 10, 2009, 02:25 PM

I have 2 IBActions, with similar sounding names.....

IB cuts the ends of the names of in the inspector panel, so I had my connections wrong!

The code posted above, does in fact work.