PDA

View Full Version : NSHTTPCookieAcceptPolicy




BollywooD
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.



BollywooD
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?

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

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

Bingo!

thanks for your help!!
:D

BollywooD
Apr 9, 2009, 09:24 PM
OK,

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?

kainjow
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...

BollywooD
Apr 10, 2009, 02:25 PM
Duh....

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.
:o