【问题标题】:Having trouble when fetching Facebook user's details with latest iOS FBSDK (July 7, 2015)使用最新的 iOS FBSDK 获取 Facebook 用户的详细信息时遇到问题(2015 年 7 月 7 日)
【发布时间】:2015-09-26 07:28:31
【问题描述】:

我已经为我的应用程序使用了自定义 UI FBLogin 按钮。在LoginHomeViewController 我有FBLogin 按钮。

这是我在ViewDidLoad中使用的代码

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];

if ([FBSDKAccessToken currentAccessToken])    
{        
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler: ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {                
        if (!error)
        {
            NSLog(@"fetched user:%@", result);
        }
    }];
}

1) 问题是当我第一次在 safari 或 FB 上没有控制台输出时,我正在登录我想要此视图控制器中的用户详细信息,即 LoginHomeViewController

2) 目前在 safari 或 FB 中完成登录后,当我返回上一个视图并进入此控制器时,即LoginHomeViewController,然后在

中获取用户详细信息
NSLog(@"fetched user:%@", result); 

我在自定义登录按钮点击中尝试了上面的代码,即

- (IBAction)facebookClcik:(id)sender {
}

但我面临同样的问题。

以下是我的 AppDelegate 方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions 
{
    [FBSDKLoginButton class];
    return [[FBSDKApplicationDelegate sharedInstance] application : application didFinishLaunchingWithOptions : launchOptions];
}

- (BOOL)application : (UIApplication *)application openURL : (NSURL *)url sourceApplication : (NSString *)sourceApplication annotation : (id)annotation
{
    return [[FBSDKApplicationDelegate sharedInstance] application : application openURL : url sourceApplication : sourceApplication annotation : annotation];
}

- (void)applicationDidBecomeActive : (UIApplication *)application
{
    [FBSDKAppEvents activateApp];
}

【问题讨论】:

  • 为什么appDelegateapplication: didFinishLaunchingWithOptions:方法里面有[FBSDKLoginButton class]方法?
  • @DharmeshDhorajiya 您的解决方案在用户已经登录时工作(在我上面的代码中也工作)。当我在同一个视图控制器中单击按钮 facebookClcik 时,我需要详细信息,即当我们从 safari 回来时 LoginHomeViewControlle。
  • @AshishKakkad 没问题,我的问题是当我在 LoginHomeViewController 中单击 facebookClcik 按钮时需要详细信息。目前,当我转到上一个视图并进入 LoginHomeViewController 时,我正在获取详细信息。单击按钮后,我需要同一控制器中的详细信息。
  • @ShrikantKankatti 这就是我所说的。保存您检索的 facebook 数据

标签: ios objective-c facebook login facebook-login


【解决方案1】:

希望这种方式可以帮助您检索用户详细信息:

 - (IBAction)facebookLoginBtn:(id)sender {
appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.logOut=  true;


FBSession *session = [[FBSession alloc] initWithPermissions:@[@"publish_actions",@"email"]];
[FBSession setActiveSession:session];

[session openWithBehavior:FBSessionLoginBehaviorForcingWebView
        completionHandler:^(FBSession *session,
                            FBSessionState status,
                            NSError *error) {
            if (FBSession.activeSession.isOpen) {
                [[FBRequest requestForMe] startWithCompletionHandler:
                 ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                     if (!error) {
                         NSLog(@"accesstoken %@",[NSString stringWithFormat:@"%@",session.accessTokenData]);
                         NSLog(@"user id %@",user.id);
                         NSLog(@"Email %@",[user objectForKey:@"email"]);
                         NSLog(@"User Name %@",user.first_name);
                         appDelegate.UserName=[NSMutableString stringWithFormat:@"%@", user.first_name];
                         appDelegate.firstname =[NSMutableString stringWithFormat:@"%@", user.first_name];
                         appDelegate.idStr= user.id;
                         UserEmailStr= [user objectForKey:@"email"];
                        // [self fetchGuestuser];
                     }
                 }];
            }
        }];


  }

如果您将用户导航到 safari,Apple 可能会拒绝您的应用

了解更多refer this

【讨论】:

  • Facebook iOS SDK 4.0 中没有 FBSession -- stackoverflow.com/questions/29282769/…
  • oh sorry..我没有检查最新的SDK "FBSession 和 FBAccessTokenData 被FBSDKLoginManager, FBSDKAccessToken 替换。" .
【解决方案2】:

试试这个代码

if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available");

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 FbId = [NSString stringWithFormat:@"%@",[result objectForKey:@"id"]];
                 firstName = [NSString stringWithFormat:@"%@",[result objectForKey:@"first_name"]];
                 lastName = [NSString stringWithFormat:@"%@",[result objectForKey:@"last_name"]];
                 UserName = [NSString stringWithFormat:@"%@",[result objectForKey:@"name"]];

                 email_id = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
                 picture = [NSString stringWithFormat:@"%@",[[[result objectForKey:@"picture"] objectForKey:@"data"] objectForKey:@"url"]];
                 Password = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
//            user_id=[NSString stringWithFormat:@"%@",[result objectForKey:@"id"]];

                 NSLog(@"email is %@", [result objectForKey:@"email"]);

                 NSLog(@"picture is %@", picture);// [result objectForKey:@"picture"]

                 [[NSUserDefaults standardUserDefaults]setObject:result forKey:@"FBData"];
                 [[NSUserDefaults standardUserDefaults]setObject:@"Yes" forKey:@"Check"];

                 [self fbLoginParseData];

             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];
    }
    else
    {
        NSLog(@"User is not Logged in");
    }

【讨论】:

  • 1) 你的解决方案也是我面临的同样的问题,一旦在 safari 或 FB 中完成登录,当我返回上一个视图并进入这个控制器时,即**.LoginHomeViewController** 然后是获取用户详细信息。我需要一次点击 - (IBAction)facebookClcik:(id)sender { }
猜你喜欢
  • 2014-05-18
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多