gpt4 book ai didi

ios - 我无法在 iOS 中从 Facebook 获取用户数据

转载 作者:行者123 更新时间:2023-12-01 17:25:58 26 4
gpt4 key购买 nike

我是 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);

}];
}
}
}

最佳答案

如果您正在查看 FacebookSDK 中 Facebook 提供的示例代码有一个样本SessionLoginSample请检查是如何工作的:-

您必须像这样配置您的应用程序:-

enter image description here

在 native 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 登录 View Controller
@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

关于ios - 我无法在 iOS 中从 Facebook 获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20263695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com