How to set / check cookies in iPhone application?

I'm trying to set a cookie, and also check if it has one, does anyone have a sample code for this? All I found is a TTPCookieStorage class reference , but it would be useful if I could see an example implementation.

+3
source share
3 answers

Only the HTTP connection server should set cookies. It does this with the Set-Cookie field in the headers.

The cookie store that you linked to processes all NSURLConnection cookie actions (both receiving and setting), and in general - you should not modify cookies yourself. If you want to override, you cannot use NSURLConnection and you will need to use CFReadStreamRef and handle the connection and manually create CFHTTPMessageRef.

You will need to process cookies if you are implementing server-side HTTP communications.

If you are using the CFHTTPMessageRef server, then:

NSDate *expiryDate = /* set some date value */

CFHTTPMessageSetHeaderFieldValue(
    response,
    (CFStringRef)@"Set-Cookie",
    (CFStringRef)[NSString stringWithFormat:
            @"SomeCookieName=%@;Path=/;expires=%@",
            someStringValue,
            [dateFormat stringFromDate:expiryDate]]);

where responseis the one CFHTTPMessageRefyou use to answer. You can use CFHTTPMessageCopyAllHeaderFieldsand get the object for the cookie key to retrieve cookies from the client in the CFHTTPMessageRef header.

+4
source

cookie -. -, UIWebView. , NSUserDefaults. cookie, , NSURLConnection . , cookie UIWebView, , NSHTTPCookieStorage.

NSHTTPCookieStorage

cookie -.

, root iPhone.

+1

Source: https://habr.com/ru/post/1744084/


All Articles