NSHTTPCookieAcceptPolicy

Discussion in 'Mac Programming' started by BollywooD, Apr 9, 2009.

  1. macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #1
    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.
     
  2. thread starter macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #2
    I have added a notification, using notification centre to: NSHTTPCookieManagerAcceptPolicyChangedNotification
    using the following code:
    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?
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    It might be a distributed notification. Try using NSDistributedNotificationCenter instead.
     
  4. thread starter macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #4
    Bingo!

    thanks for your help!!
    :D
     
  5. thread starter macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #5
    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:

    Code:
    - (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?
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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...
     
  7. thread starter macrumors 6502

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #7
    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.
    :eek:
     

Share This Page