我无法在 iOS 中从 Facebook 获取用户数据
Posted
技术标签:
【中文标题】我无法在 iOS 中从 Facebook 获取用户数据【英文标题】:I am not able to fetch user data from Facebook in iOS 【发布时间】:2013-11-28 10:35:21 【问题描述】:我是 iPhone 编程的新手。使用下面的代码,我登录 Facebook 并获取数据。但这里的问题是,如果单击 Loginwithfacebook 按钮 Facebook 对话框打开。然后如果再次输入 Facebook 的用户名和密码单击相同的按钮,然后我正在获取用户数据。但是我不想单击两次。而且一旦我登录 Facebook.inside 应用程序,我当时想获取用户数据我不登录再一次,直到我点击注销。请给我最好的主意。
-(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);
];
【问题讨论】:
【参考方案1】:如果您正在查看 Facebook 在FacebookSDK
中提供的示例代码,则有一个示例SessionLoginSample请检查是如何工作的:-
你必须像这样配置你的应用程序:-
在本机 ios 应用程序中,您必须设置您在项目中使用的应用程序包 ID,例如(com.compame.projecname)
为了在您的项目中实施,我在这里解释一下。
在 .h 文件中使用IBOutlet
创建一个按钮,然后像往常一样将此按钮连接到 xib
#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
在 .m 文件中 @implementation 登录视图控制器 @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);
注意:-
如果您尝试使用FBProfilePictureView
设置登录用户的图像,请不要忘记将[FBProfilePictureView class];
添加到应用程序代表中,如贝娄
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[FBProfilePictureView class];
// Override point for customization after application launch.
return YES;
这是给你的示例代码 Sample code Facebook log-in with Image
【讨论】:
非常感谢@nitin 你伟大的 Nithinva :)))) @Nithin Gohel【参考方案2】:如果你愿意,你也可以用 Parse 来做:
- (IBAction)facebookRegistrationButtonPressed
// The permissions requested from the user
NSArray *permissionsArray = @[@"email", @"user_about_me", @"publish_actions", @"status_update", @"manage_pages", @"publish_stream"];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error)
if (!user)
if (!error)
NSLog(@"Uh oh. The user cancelled the Facebook login.");
else
NSLog(@"Uh oh. An error occurred: %@", error);
else if (user.isNew)
NSLog(@"User with facebook signed up and logged in!");
[self processFacebookRequest];
else
NSLog(@"User with facebook logged in!");
[self processFacebookRequest];
];
- (void)processFacebookRequest
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
if (!error)
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
NSString * email = [result objectForKey:@"email"];
NSString *facebookID = userData[@"id"];
NSLog(@"facebookID = %@", facebookID);
else
NSLog(@"error : %@", error.localizedDescription);
];
【讨论】:
感谢重播。PFFacebookUtils 是什么?PFFacebookUtils
是与 Parse 框架关联的类。不是重播,而是使用 Parse 框架的替代方案:www.parse.com以上是关于我无法在 iOS 中从 Facebook 获取用户数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在统一引擎中从 Facebook API 获取用户名和用户个人资料图片?
如何在 ios 中从 facebook 获取朋友的最新状态?