How to maintain and clear sessions in a UIwebview? - iphone

How to maintain and clear sessions in a UIwebview in iphone sdk

Just delete the cookies from the application. I used this for FBGraph as I was unable to logout from Facebook. You can use this.
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#“yourDomainName"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}

Related

How to clear the cookies for foursquare in iphone application?

I am using foursquare in my application but I can't to sign-out from the foursquare in my application.
Use below code to remove foursquare cookie from app
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"foursquare"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
You need to call this wherever you want to just like
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if ([cookie.name isEqualToString:#"FourSquare"]) {//its temprory name for your understanding
[cookies deleteCookie:cookie];
};
}

How can logout foursquare in iphone integration?

How the foursquare Logout functionality has to be implemented in iPhone integration application?
I tried this code.But it is not working.
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString *domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"https://foursquare.com/"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}

iPhone: Facebook logout feature does not working

I am implementing Facebook integration using FBConnct and it works fine but when I
want to log out from the Facebook it doesn't work.
My code is as follows:
- (IBAction)logOutbuttonPressed:(id)sender {
[Facebook logout:self];
}
- (void)fbDidLogout {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
NSLog(#" after %#",facebook.accessToken);
NSLog(#" date%#",facebook.expirationDate);
[defaults synchronize];
}
- (void)logout:(id<FBSessionDelegate>)delegate {
[self logout];
if (delegate != self.sessionDelegate &&
[delegate respondsToSelector:#selector(fbDidLogout)]) {
[delegate fbDidLogout];
}
+(void)fbDidLogout
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"])
{
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
// Hide the publish button.
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];
}
}
}
You need to implement this code for facebook logout as well
- (void)fbDidLogout {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
NSLog(#" after %#",facebook.accessToken);
NSLog(#" date%#",facebook.expirationDate);
[defaults synchronize];
// Finding the Facebook Cookies and deleting them
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
fbGraph = nil;
}
Try this
import 'FBConnect.h'
in ur second view controller
then .......
FBSession *session = [FBSession session]; [session logout];
Call this method to logout from facebook.
- (void)logOutFB {
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];
}
}
}

Facebook Connect iPhone API logout not working

I am attempting to write a Facebook integration in an iPhone app I'm working on. I have it logging in just fine, but I don't like the idea of being able to turn a feature on without being able to turn it off. So, in working on the logout functionality, I have been caught in a snag.
- (IBAction) logoutClicked:(id)sender {
if (fbLoggedIn)
{
FBSession * mySession = [FBSession session];
[mySession logout];
}
}
- (void)sessionDidLogout:(FBSession*)session
{
NSLog(#"Session logged out.");
[theLoginButton setTitle:#"Facebook Time!" forState:UIControlStateNormal];
fbLoggedIn = FALSE;
theLogoutButton.enabled = NO;
theLogoutButton.alpha = 0;
}
The logoutClicked method responds to a button in my xib. The delegate method is not getting called. I have tried setting the Facebook session as a property in my ViewController in order to store/access the data across methods, but that didn't seem to work either. Anybody have any solutions?
Is the sessionDidLogout implemented in a class which implements FBSessionDelegate?
And is it an instance of that class that you passed as a delegate when creating the session with the method [FBSession sessionForApplication:#"XXX" secret:#"YYY" delegate:(DELEGATE)] ?
This works for me:
(void)logout {
//self.sessionDelegate = delegate;
appDelegate.facebook.accessToken = nil;
appDelegate.facebook.expirationDate = nil;
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
NSLog(#"Log out");
// Remove saved authorization information if it exists
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:#"FBAccessTokenKey"]) {
[userDefaults removeObjectForKey:#"FBAccessTokenKey"];
[userDefaults removeObjectForKey:#"FBExpirationDateKey"];
[userDefaults synchronize];
}
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];
}
}
}
Put this code for logout. I got this from this link.
- (void) fbDidLogout {
NSLog(#"Log out");
// Remove saved authorization information if it exists
if ([userDefaults objectForKey:#"FBAccessTokenKey"]) {
[userDefaults removeObjectForKey:#"FBAccessTokenKey"];
[userDefaults removeObjectForKey:#"FBExpirationDateKey"];
[userDefaults synchronize];
}
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];
}
}
}

Where are an UIWebView's cookies stored?

I'm building an iPhone app with cookies. Deleting cookies in the Safari settings doesn't delete them. Where are they stored? Is it possible to read them from another UIWebView?
Thanks!
Your application has its own "cookie jar" in the [NSHTTPCookieStorage sharedHTTPCookieStorage] container.
Here's how you might take a quick look at the cookies in your application's cookie jar:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
NSLog(#"%#", cookie);
}
Several methods are available for filtering and manipulation. Take a look at the NSHTTPCookieStorage documentation for accessing cookies, and the NSHTTPCookie documentation for accessing individual cookie properties.
Thanks for the pointer Alex! To add to this I will drop in my "cookie dumper" that I created using Alex's example. Maybe this will help someone else.
- (void) dumpCookies:(NSString *)msgOrNil {
NSMutableString *cookieDescs = [[[NSMutableString alloc] init] autorelease];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
[cookieDescs appendString:[self cookieDescription:cookie]];
}
NSLog(#"------ [Cookie Dump: %#] ---------\n%#", msgOrNil, cookieDescs);
NSLog(#"----------------------------------");
}
- (NSString *) cookieDescription:(NSHTTPCookie *)cookie {
NSMutableString *cDesc = [[[NSMutableString alloc] init] autorelease];
[cDesc appendString:#"[NSHTTPCookie]\n"];
[cDesc appendFormat:#" name = %#\n", [[cookie name] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[cDesc appendFormat:#" value = %#\n", [[cookie value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[cDesc appendFormat:#" domain = %#\n", [cookie domain]];
[cDesc appendFormat:#" path = %#\n", [cookie path]];
[cDesc appendFormat:#" expiresDate = %#\n", [cookie expiresDate]];
[cDesc appendFormat:#" sessionOnly = %d\n", [cookie isSessionOnly]];
[cDesc appendFormat:#" secure = %d\n", [cookie isSecure]];
[cDesc appendFormat:#" comment = %#\n", [cookie comment]];
[cDesc appendFormat:#" commentURL = %#\n", [cookie commentURL]];
[cDesc appendFormat:#" version = %d\n", [cookie version]];
// [cDesc appendFormat:#" portList = %#\n", [cookie portList]];
// [cDesc appendFormat:#" properties = %#\n", [cookie properties]];
return cDesc;
}
Alex had a great idea about putting this in a category. Here's what I ended up using:
NSHTTPCookieStorage+Info.h
#import <Foundation/Foundation.h>
#interface NSHTTPCookieStorage (Info)
+ (NSDictionary*) describeCookies;
+ (NSDictionary *) describeCookie:(NSHTTPCookie *)cookie;
#end
NSHTTPCookieStorage.m
#implementation NSHTTPCookieStorage (Info)
+ (NSDictionary*) describeCookies {
NSMutableDictionary *descriptions = [NSMutableDictionary new];
[[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] enumerateObjectsUsingBlock:^(NSHTTPCookie* obj, NSUInteger idx, BOOL *stop) {
[descriptions setObject:[[self class] describeCookie:obj] forKey:[[obj name] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}];
NSLog(#"Cookies:\n\n%#", descriptions);
return descriptions;
}
+ (NSDictionary *) describeCookie:(NSHTTPCookie *)cookie {
return #{#"value" : [[cookie value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
#"domain" : [cookie domain] ? [cookie domain] : #"n/a",
#"path" : [cookie path] ? [cookie path] : #"n/a",
#"expiresDate" : [cookie expiresDate] ? [cookie expiresDate] : #"n/a",
#"sessionOnly" : [cookie isSessionOnly] ? #1 : #0,
#"secure" : [cookie isSecure] ? #1 : #0,
#"comment" : [cookie comment] ? [cookie comment] : #"n/a",
#"commentURL" : [cookie commentURL] ? [cookie commentURL] : #"n/a",
#"version" : #([cookie version]) };
}
#end
Makes the output a bit more "JSON-y"...
in sandbox:Library->Cookies->Cookies.binarycookies
but you can not open the .binarycookies directly, you can run a script:
Download and install Python
Download BinaryCookieReader.py
Run "Python BinaryCookieReader.py" on the terminal
as you can see, output log contains detail cookies description