I'm trying to create an login in my iPhone App.
NSURL *urlNew = [NSURL URLWithString:urlstring];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:urlNew];
NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody: httpbody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
[connection start];
Thats the way how I post my data to the Server, it works - i get a response. The problem is, that the response is the code of the Login-side, not the code of the "saved" area. I think I have to create cookies, so that the website knows, that I am logged in.
I searched the internet but didn't found anything useful. So how do I create a cookie, when I am loggin me in? Do I have to post this cookie everytime, when I am going to another link in the "saved" area?
Hope you unterstand my problem.
Greetz
There is source code to extract cookie string from
NSHTTPURLResponse
:And to set cookie string to
NSMutableURLRequest
:not sure it's still relevant, but in case someone find that useful, here's how you get the cookies after making a request. You should implement the selector
connectionDidFinishLoading:
in the object that was specified as a delegate toNSURLConnection
(self in your case):in this connectionDidFinishLoading: selector you can access the cookies like that:
then later, this stored value can be used in requests like this:
or more advanced
setAllHTTPHeaderFields:
, but remember to use the correct value inNSHTTPCookiePath
field, see details herealso
NSHTTPCookieStorage
has selector-setCookies:forURL:mainDocumentURL:
that can also be used to set cookies.I had the same problem with a WKWebView showing the cookie on a PHP page on initial load but the cookie was cleared when page was reloaded/refreshed. Here's what I did to make it work.
My WKWebView with the cookie setting:
On my phpwebsitewithcookiehandling.com I used the following to verify it worked:
It worked for me after much trial and error. Good luck. :)
Try this
Edit:
OP wants to know how to create a cookie. So here is some code