Edit: adapting for edited question
NSHTTPCookieStorage has a -setCookies:forURL:mainDocumentURL: method, so the easy thing to do is use NSURLConnection and implement -connection:didReceiveResponse:, extracting cookies and stuffing them into the cookie jar:
- ( void )connection: (NSURLConnection *)connection
didReceiveResponse: (NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSArray *cookies;
cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields:
[ httpResponse allHeaderFields ]];
[[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
setCookies: cookies forURL: self.url mainDocumentURL: nil ];
}
(You can also simply extract an NSDictionary object from the NSHTTPCookie with properties, and then write the dictionary to the disk. Reading it back in is as easy as using NSDictionary's -dictionaryWithContentsOfFile: and then creating the cookie with -initWithProperties:.)
Then you can pull the cookie back out of the storage when you need it:
- ( void )reloadWebview: (id)sender
{
NSArray *cookies;
NSDictionary *cookieHeaders;
NSMutableURLRequest *request;
cookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
cookiesForURL: self.url ];
if ( !cookies ) {
/* kick off new NSURLConnection to retrieve new auth cookie */
return;
}
cookieHeaders = [ NSHTTPCookie requestHeaderFieldsWithCookies: cookies ];
request = [[ NSMutableURLRequest alloc ] initWithURL: self.url ];
[ request setValue: [ cookieHeaders objectForKey: @"Cookie" ]
forHTTPHeaderField: @"Cookie" ];
[ self.webView loadRequest: request ];
[ request release ];
}