脸书登录iOS



我有一个应用程序,我登录到Facebook并获取public_profile然后显示名字和图片。 就像我说的,我通过使用FBSDKProfilePictureView使图片正常工作。

现在唯一的问题是,当我进入视图并且我之前登录时,会出现图片和名称。 我点击登录按钮,图片变为轮廓,名称变为无。 再次点击登录按钮图片和名称好。

但是,如果我在未登录时进入视图,则名称为无,图片轮廓正常。 我点击了登录按钮,我得到了 错误:无法完成操作。(com.facebook.sdk.core error 8。 图片显示,但名称保持不变 如果我点击登录按钮注销,图片就会消失,名称是用户的名字,并且我收到一个日志,表明登录按钮没有错误。 点击登录按钮登录,图片就在那里,名称保持名字,之后一切都很好。 这是我的相关代码。

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
@interface videoUploadTestViewController () <FBSDKLoginButtonDelegate>
@property (strong, nonatomic) FBSDKLoginButton *loginButton;
@property (strong, nonatomic) FBRequestConnection *requestConnection;
@property (strong, nonatomic) IBOutlet UILabel *labelFirstName;
@property (strong,nonatomic)IBOutlet FBSDKProfilePictureView *profilePictureView;
@property (strong,nonatomic) FBSDKLoginManager *loginManager;
- (IBAction)buttonRequestClickHandler:(id)sender;
@synthesize loginManager = _loginManager;
@synthesize loginButton = _loginButton;
@synthesize requestConnection = _requestConnection;
@synthesize labelFirstName = _labelFirstName;
- (void)dealloc {
[_requestConnection cancel];
}
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear");
if ([_labelFirstName.text isEqualToString:@"none"]) {}
else{
self.loggedInString = @"logged in";
}
if ([loggedInString isEqualToString:@"logged in"]) {
[_loginButton
addTarget:self
action:@selector(logoutButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
else if ([loggedInString isEqualToString:@"logged out"]) {
[_loginButton
addTarget:self
action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
NSLog(@"loggedinstring:%@",loggedInString);
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad");
_loginButton=[FBSDKLoginButton buttonWithType:UIButtonTypeCustom];
_loginButton.backgroundColor=[UIColor darkGrayColor];
_loginButton.frame=CGRectMake(0,150,180,28);
[self.view addSubview:_loginButton];
loggedInString = @"logged out";
//THIS IS WHERE I GET INFO IF ALREADY SIGNED IN
if ([FBSDKAccessToken currentAccessToken]) {
NSLog(@"currentAccessTokenviewDidLoad");
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,first_name,picture.width(150).height(150)"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched userviewdidload:%@", result);
NSString *helloString = NSLocalizedString(@"HELLO", nil);
NSString *userFirst = [helloString stringByAppendingString:[result objectForKey:@"first_name"] ];
self.labelFirstName.text = [userFirst stringByAppendingString:@"!"];
firstName =[result objectForKey:@"first_name"];
NSDictionary *dictionary = (NSDictionary *)result;
NSDictionary *data3 = [dictionary objectForKey:@"picture"];
NSDictionary *data2 = [data3 objectForKey:@"data"];
NSString *photoUrl = (NSString *)[data2 objectForKey:@"url"];
FBSDKProfilePictureView *profilePictureView = [[FBSDKProfilePictureView alloc] init];
profilePictureView = [dictionary objectForKey:@"picture"];
}
}];
}
else {
NSLog(@"firstNameviewDidLoad;%@",firstName);
NSLog(@"noaccesstoken");

self.labelFirstName.text = @"none";
}

-(void)loginButtonClicked
{
self.loggedInString = @"logged in";
NSLog(@"loginclicked");
/*_loginManager = [[FBSDKLoginManager alloc] init];*/
[_loginManager
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
}
else if (result.isCancelled) {
NSLog(@"Cancelled");
}
else {
NSLog(@"Logged in");
if(result.token)   // This means if There is current access token.
{
NSLog(@"token"); // Token created successfully and you are ready to get profile info
}
}
}];
if ([loggedInString isEqualToString:@"logged in"]) {
[_loginButton
addTarget:self
action:@selector(logoutButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
else if ([loggedInString isEqualToString:@"logged out"]) {
[_loginButton
addTarget:self
action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}

NSLog(@"logintext2;%@",_loginButton.titleLabel.text);
[self getFacebookProfileInfos];
}

-(void) logoutButtonClicked
{ self.loggedInString = @"logged out";
NSLog(@"logoutclicked");
self.labelFirstName.text = @"none";
if ([loggedInString isEqualToString:@"logged in"]) {
[_loginButton
addTarget:self
action:@selector(logoutButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
else if ([loggedInString isEqualToString:@"logged out"]) {
[_loginButton
addTarget:self
action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}


}
-(void)getFacebookProfileInfos {
NSLog(@"firstName%@",firstName);
if (firstName == NULL) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,first_name,picture.width(150).height(150)"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
// THIS IS WHAT SHOWS UP WHEN I CLICK SIGN OUT AFTER SIGNING IN FIRST TIME
NSLog(@"noerror");
firstName =[result objectForKey:@"first_name"];
NSString *helloString = NSLocalizedString(@"HELLO", nil);
NSString *userFirst = [helloString stringByAppendingString:firstName ];
self.labelFirstName.text = [userFirst stringByAppendingString:@"!"];
}
else{
NSLog(@"firstnameerror:%@",firstName);
NSLog(@"Error: %@", error.localizedDescription);
[self.view setNeedsDisplay];
}
}];
}
else {
NSLog(@"currentAccessTokenprofileinfos");
NSString *helloString = NSLocalizedString(@"HELLO", nil);
NSString *userFirst = [helloString stringByAppendingString:firstName ];
self.labelFirstName.text = [userFirst stringByAppendingString:@"!"];
}
}

我真的开始感到沮丧了。 任何帮助将不胜感激

这是更新 UI 以响应 Web 服务请求时的常见问题。

在用户登录后FBSDK调用的闭包中,您似乎正在更新运行Web请求的同一线程上的UI(facebookImage,labelFirstName)。这可能不是(通常不是)可以访问 UI 的"主线程"。

尝试将这些 UI 更新调度到主线程。这将对更新进行排队,下次主线程运行时,它将执行您的更新。

dispatch_async(dispatch_get_main_queue(), ^{
self.labelFirstName.Text = // etc, etc;
});

最新更新