我无法在iOS中从Facebook获取用户数据 [英] I am not able to fetch user data from Facebook in iOS

查看:167
本文介绍了我无法在iOS中从Facebook获取用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone编程的新手。使用下面的代码我登录到Facebook和获取数据。但是这里的问题是如果点击Loginwithfacebook按钮Facebook对话框打开。我输入Facebook的用户名和密码后,如果再次点击相同的按钮,然后我得到用户数据。但我不想点击双倍的时间。并且一旦我登录Facebook.inside的应用程序,我想要获取用户数据当时我不登录再一次,直到我点击注销。请给我最好的想法。

   - (void)LoginWithFacebookk:(id)sender 
{
if(!FBSession.activeSession.isOpen){

FBSession * session = [[FBSession alloc] init];
//设置活动会话
[FBSession setActiveSession:session];
//打开会话
[session openWithBehavior:FBSessionLoginBehaviorForcingWebView
completionHandler:^(FBSession * session,
FBSessionState status,
NSError * error)
{


}];

}
else
{
NSLog(@open response);
FBRequest * friendsRequest = [FBRequest requestForMe];
[friendsRequest startWithCompletionHandler:^(FBRequestConnection * connection,NSDictionary< FBGraphUser> * user,NSError * error){

NSString * emailiddd = [NSString stringWithFormat:@%@, user objectForKey:@email]];

NSLog(@%@,emailiddd);
NSString * post = [NSString stringWithFormat:@email =%@,emailiddd];

NSLog(@%@,post);

}];
}
}
}


解决方案

如果您在 FacebookSDK 中检查Facebook提供的示例代码,则有一个示例


I am new to iPhone programming.Using below code i am login into Facebook and fetching data.But here problem is if click on the Loginwithfacebook button Facebook dialog box opening.there i am enter username and password of Facebook after that if again click on same button then i am getting user data.But i don't want to click on double time.And also once i login in Facebook.inside the app where i want to fetch user data at that time i don't to login one more time until i click on logout.Please give me best idea.

-(void)LoginWithFacebookk:(id)sender
{ 
   if (!FBSession.activeSession.isOpen) {

    FBSession *session = [[FBSession alloc] init];
    // Set the active session
    [FBSession setActiveSession:session];
    // Open the session
    [session openWithBehavior:FBSessionLoginBehaviorForcingWebView
            completionHandler:^(FBSession *session,
                                FBSessionState status,
                                NSError *error)
     {


            }];

        }
        else
        {
            NSLog(@"open response");
            FBRequest* friendsRequest = [FBRequest requestForMe];
            [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary <FBGraphUser> *user,NSError *error) {

                NSString *emailiddd=[NSString stringWithFormat:@"%@",[user objectForKey:@"email"]];

                NSLog(@"%@",emailiddd);
                NSString *post = [NSString stringWithFormat:@"email=%@",emailiddd];

                NSLog(@"%@",post);

     }];
               }
    }
}

解决方案

if you are checking sample code of provide by Facebook in FacebookSDK there is one sample SessionLoginSampleplease check how did work:-

You have to configur you app like this:-

in Native iOS app you have to set you app Bundle Id that you are using in to you Project for example(com.compame.projecname)

For implement in to your Project Here i put the explain.

in .h file create One Button with IBOutlet and connect this button in to xib as we did normally

#import <FacebookSDK/FacebookSDK.h>


@interface LoginViewController : UIViewController<FBLoginViewDelegate>
{
    IBOutlet UIButton *FB_login; // just connect IBoutlate no need to give IBAction
    FBLoginView *loginview;

}
@property (strong, nonatomic) id<FBGraphUser> loggedInUser;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePic; //this is for show profile pic or logged in user

in .m File @implementation LoginViewController @synthesize profilePic = _profilePic;

- (void)viewDidLoad
{
    loginview = [[FBLoginView alloc] init];
    loginview =
    [[FBLoginView alloc] initWithPublishPermissions:[NSArray arrayWithObjects:@"publish_actions",@"email",nil] defaultAudience:FBSessionDefaultAudienceFriends];

    loginview.frame =FB_login.frame;//CGRectMake(90,149, 280, 55);
    for (id obj in loginview.subviews)
    {
        if ([obj isKindOfClass:[UIButton class]])
        {
            UIButton * loginButton =  obj;

            UIImage *loginImage = [UIImage imageNamed:@"btn_fb_login.png"];
            [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
            [loginButton setBackgroundImage:nil forState:UIControlStateSelected];
            [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
            [loginButton sizeToFit];
        }
        if ([obj isKindOfClass:[UILabel class]])
        {
            UILabel * loginLabel =  obj;
            loginLabel.text =@""; //@"Log in to facebook";
            loginLabel.textAlignment = NSTextAlignmentCenter;
            loginLabel.frame =CGRectMake(123,149, 280, 55);// CGRectMake(0, 0, 271, 37);
        }
    }

    loginview.delegate = self;

    [self.view addSubview:loginview];

    [super viewDidLoad];

}

-(void)LogOut:(NSNotification *)notif
{
    [self loginViewShowingLoggedOutUser:loginview];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *each in cookieStorage.cookies) {
        // put a check here to clear cookie url which starts with twitter and then delete it
        [cookieStorage deleteCookie:each];
    }
}

#pragma  mark------ FaceBook Sign In method------------

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {

    NSLog(@"Logged In");


}

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {

    NSLog(@"%@",user);
    self.profilePic.profileID = user.id;





}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
    // Called after logout
    NSLog(@"Logged out");

    self.profilePic.profileID = nil;
    [FBSession.activeSession closeAndClearTokenInformation];


}
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
    // see https://developers.facebook.com/docs/reference/api/errors/ for general guidance on error handling for Facebook API
    // our policy here is to let the login view handle errors, but to log the results
    NSLog(@"FBLoginView encountered an error=%@", error);
}

NOTE:- If you are trying to set Image of logged in user using FBProfilePictureView do not forget add [FBProfilePictureView class]; in to app Delegate like Bellow

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [FBProfilePictureView class];
    // Override point for customization after application launch.
    return YES;
}

Here it is a sample code for you Sample code Facebook log-in with Image

这篇关于我无法在iOS中从Facebook获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆