1

I am writing an app that needs to store a cookie.

my plan is to do the following.

// create a ref to shared storage area

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

// query the current acceptpolicy (ie want to store it for later...)

[cookieJar cookieAcceptPolicy];       /********   This line crashes app   ********/

[cookieJar setcookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

NSArray *mycookies = [cookieJar cookies];

.....  do more stuff
..... change it back....

Problem is I can't figure out how to store the result of cookieAcceptPolicy for later use. the docs shows - (NSHTTPCookieAcceptPolicy)cookieAcceptPolicy, makes no sense to me.

Help appreciated.

1
  • "Crashes app" is useless, run it under the debugger and find out why it's crashing.
    – Nick Veys
    Dec 3, 2009 at 19:15

1 Answer 1

2

The documentation actually shows - (NSHTTPCookieAcceptPolicy)cookieAcceptPolicy which means it returns the value of an enum typedef'd as NSHTTPCookieAcceptPolicy. So something like:

NSHTTPCookieAcceptPolicy currentPolicy = [cookieJar cookieAcceptPolicy];
[cookieJar setCookieAcceptPolicy: NSHTTPCookieAcceptPolicyAlways];

//do stuff

//change it back
[cookieJar setCookieAcceptPolicy: currentPolicy];

should work. The reason your app is "crashing" is because of the lowercase 'c' in the setCookieAcceptPolicy method of the line below the one you think is crashing your app. You definitely receive a warning about this at compile time... in the future I'd advise you to strive for not just error-free, but warning-free objective-C code, since type-checking isn't plausible for dynamically-typed languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.