我正在创建一个简单的iOS应用程序,登录到Facebook,获取自己和朋友的详细信息,从Facebook注销。
现在从Facebook登录并获取自我和朋友的详细信息工作正常,但我无法从Facebook完全注销。每当我退出然后登录回来 - 我都会看到授权屏幕,而不是Facebook(网页)的身份验证登录屏幕。
以下是我的代码 - 你可以看看我从登录,到获取自我和我的步骤中是否有什么问题。朋友的详细信息和从Facebook登出
1)使用以下方法登录Facebook
- (void)openSession
{
if(FBSession.activeSession.isOpen)
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
else
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
{
// Connection is Open
lblStatus.text = @"FBSessionStateOpen";
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
{
[FBSession.activeSession closeAndClearTokenInformation];
// Connection is Closed / Login Failed
lblStatus.text = @"FBSessionStateClosed";
}
break;
default:
break;
}
}
2)使用以下方法
获取自我详细信息和朋友详细信息if (FBSession.activeSession.isOpen)
{
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
self.lblSelfDetails.text = user.name;
self.profilePicture.profileID = user.id;
}
}];
}
FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,birthday"];
[friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSArray *data = [result objectForKey:@"data"];
for (FBGraphObject<FBGraphUser> *friend in data)
{
NSLog(@"%@:%@", [friend name],[friend birthday]);
}
}];
3)使用以下方法退出
- (IBAction)logout:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];
}
答案 0 :(得分:1)
请使用此方法从Facebook成功登录
- (void)logout:(id<FBSessionDelegate>)delegate {
_sessionDelegate = delegate;
NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
[self requestWithMethodName:@"auth.expireSession"
andParams:params andHttpMethod:@"GET"
andDelegate:nil];
[params release];
[_accessToken release];
_accessToken = nil;
[_expirationDate release];
_expirationDate = nil;
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:@"https://m.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogout)]) {
[_sessionDelegate fbDidLogout];
}
}
希望这可以帮助你!!!
答案 1 :(得分:0)
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
然后在.m文件中添加这些方法
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController *topViewController =
[self.navController topViewController];
if ([[topViewController modalViewController]
isKindOfClass:[SCLoginViewController class]]) {
[topViewController dismissModalViewControllerAnimated:YES];
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
[self.navController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
接下来,创建一个新方法,关闭当前会话并将人员记录下来:
-(void)logoutButtonWasPressed:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];
}