facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXXXX" andDelegate:self]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]){ facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; } NSLog(@"startup login"); [self loginToFacebook];//attempt to login automatically on startup
我的loginToFacebook函数是这样的:
- (void)loginToFacebook { NSLog(@"Logging into facebook"); //set up facebook and login in automatically if possible NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]){ facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; } if (![facebook isSessionValid]){ //get permissions that user will need to agree to us using NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_likes",@"read_friendlists",@"offline_access",@"publish_stream",nil]; //authorise our login [facebook authorize:permissions]; [permissions release]; } else { [facebook authorize:nil]; } }
就我而言,这次我应该被提示用我的电子邮件和密码登录.
我主要使用模拟器,因此Facebook应用程序本身不是原因,而在Facebook.h类中我已经改变了
[self authorizeWithFBAppAuth:NO safariAuth:YES];
至
[self authorizeWithFBAppAuth:NO safariAuth:NO];
然而,发生的事情是facebook窗口短暂地闪烁并消失,正如我预期的那样,当我已经登录时.它然后从我之前的登录详细信息加载我的id,姓名,朋友列表等.我不再可能改变用户!我应该补充一点,我有以下代码,相应地打印’注销’:
- (void)fbDidlogout{ NSLog(@"Logged out of facebook"); }
…在调用[facebook logout]时打印到控制台.我也有正确的网址方案,所以没有问题.
正如我所说,我已经在另一个应用程序上工作,但我看不出这次我可以忽略的东西.
我欢迎任何建议,因为我怀疑这是一件非常简单的事情.
编辑:我刚刚在设备上测试了它,尝试登录Facebook会导致应用程序崩溃.我怀疑这是因为它试图使用’存储’信息登录,因为我尚未登录设备,因此该信息不存在.我还在调查,但如果有任何可以看到一个明显的缺陷,我会非常感激.
Edit2:我尝试使用safecase的示例删除cookie.我还在a)didFinishLaunchingWithOptions和b)fbDidlogout期间打印出所有的cookie,我得到以下内容:
一个)
`2012-05-18 10:40:40.665 MyApp [15545:17003](
“”
"<NSHTTPCookie version:0 name:\"c_user\" value:\"634361620\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:TRUE>","<NSHTTPCookie version:0 name:\"csm\" value:\"2\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>","<NSHTTPCookie version:0 name:\"datr\" value:\"gxe2T8ZyBzMGb5w3LS29Q0kJ\" expiresDate:2014-05-18 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>","<NSHTTPCookie version:0 name:\"locale\" value:\"en_US\" expiresDate:2012-05-25 09:36:43 +0000 created:2012-05-18 09:36:44 +0000 (3.59027e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>","<NSHTTPCookie version:0 name:\"lu\" value:\"RgayA7CMIlsAl-lOD2-Y-O3g\" expiresDate:2014-05-18 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>","<NSHTTPCookie version:0 name:\"m_user\" value:\"0%3A0%3A0%3A0%3Av_1%2Cajax_1%2Cwidth_320%2Cpxr_1%2Cgps_1%3A1337333673%3A2\" expiresDate:2012-08-16 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>","<NSHTTPCookie version:0 name:\"s\" value:\"Aa4WdKU-oOFathmK\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:TRUE>","<NSHTTPCookie version:0 name:\"xs\" value:\"125%3AFFIWXjAXDXUMmw%3A2%3A1337333673\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:TRUE>"
)`
b)
2012-05-18 10:41:16.530 MyApp [15545:17003](
)
由于它变为空,我认为它们被删除了.但是在重新打开应用程序时,它们又在那里.
编辑3:我发现的唯一方法是在应用程序打开后立即删除所有cookie,但这意味着用户必须每次登录,即使他们在上次打开应用程序时自己登录.这是暂时的修复,我仍然不确定它为什么不能正常工作.
@R_404_323@
- (void)fbDidlogout { NSLog(@"Logged out of facebook"); 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]; } } }