cookies and session in iPhone - iphone

I want to set session and store values like cookies in the web browser in iPhone App. Is it possible? How can I implement this in iPhone? Can any one suggest a good solution..

Just set cookies like
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSHTTPCookie* testCookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
#"1", NSHTTPCookieValue,
#"test_cookie", NSHTTPCookieName,
#".facebook.com", NSHTTPCookieDomain,
#"/", NSHTTPCookiePath,
nil]];
[cookies setCookie:testCookie];
And Get the cookies like
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
//here you can get you specific cookies
}

Related

Basic authentication logout on iPhone

I have run into a little problem. I am connecting to a webservice that use Basic authentication and now I want the user to either: be able to log out, or that each time the request is made a new authentication is made. Right now the authentication seems to be cached. How do I do this?
I tried to append # at the end, but that did not seem to do the trick. The URL has the following format:
www.example.org/mywebservice/data
NSURL* url = [NSURL URLWithString: #"www.example.org/mywebservice/data"];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection* newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.myConnection = newConnection;
[request release];
[newConnection release];
I think you want to clear the cache of the previous requests and every time want to do new request at login. So for that just put below code before doing request for login
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
[[NSURLCache sharedURLCache] removeAllCachedResponses];
May help you
Happy Coding :)
I got it to work. I changed NSURLCredentialPersistenceSession to NSURLCredentialPersistenceNone.
NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser:user.username password:user.password
persistence:NSURLCredentialPersistenceNone] autorelease];
Have you tried clearing the Authentication header in your NSURLRequest?
[urlRequest setValue:#"" forHTTPHeaderField:#"Authorization"];

How to clear cache and cookies in iphone

In my iPhone application I want to clear the cache and cookies at the logout page. Please give proper suggestions for that. Now I am using the below code, but it is not working properly.
[[NSURLCache sharedURLCache] removeAllCachedResponses];
I've been able to make it work using NSHTTPCookie and NSHTTPCookieStorage
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"]; //i used this to remove facebook related cookie so gave the domain name as facebook
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
I've never actually worked with clearing cache, but used a piece of code which disables caching
NSURLCache *disableCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:disableCache];
[disableCache release];

How to manage Secure cookie with ASIHTTPRequest?

In my ipad project i am using ASIHttpRequest to handle my webservice calls. I am also doing cookie management. My webservice calls are worked fine until i have changed my service cookies all into Secure one.
How to manage Secure cookie with ASIHTTPRequest?
Thanks!
According to the docs:
You can turn off useCookiePersistence, and manage the set of cookies for a particular request manually.
//Create a cookie
NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:[#"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
[properties setValue:#"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
[properties setValue:#".allseeing-i.com" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:#"/asi-http-request/tests" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//This url will return the value of the 'ASIHTTPRequestTestCookie' cookie
url = [NSURL URLWithString:#"https://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"];
request = [ASIHTTPRequest requestWithURL:url];
[request setUseCookiePersistence:NO];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
[request startSynchronous];
//Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
NSLog(#"%#",[request responseString]);
To make the cookie secure, just add this to the properties dictionary:
[properties setValue:#"TRUE" forKey:NSHTTPCookieSecure];
Just remember, secure cookies will only be used with HTTPS requests.

Facebook Integration, iPhone

Facebook is not integrating with my application. even the sample codes are not working
Is facebook having some problem with iOS because the documentation and sample codes on developer.facebook also not found.
use this sharekit
it can integrate facebook, twitter, and much more apis
Try this new facebook grpah api
code for login on any button
fbGraph.accessToken = nil;
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
[self loginButtonPressed:nil];

App to display safari cookies on iphone

I want to make an app in iphone such that it displays the history of safari browser in iphone. Means I want to access safari cookies through other app. This is the first time m placing a query on overflow...... can anybody please let me know about this... please do reply
i don't know whether it will work or not but you can try this using NSHTTPCookie and NSHTTPCookieStorage classes...
code would be similar this
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSURL *URL;
NSArray *cookies;
NSString *cookieString = #"";
cookies = [cookieStorage cookies];
if([cookies count] > 0) {
NSHTTPCookie *cookie =[cookies objectAtIndex:0];
cookieString = [NSString stringWithFormat: #"%#=%#", [cookie name], [cookie value]];
NSLog(cookieString);
}