iPhone Cookie Management
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.
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.
精彩评论