gpt4 book ai didi

ios - FBSession 必须指定解析

转载 作者:行者123 更新时间:2023-11-29 12:30:54 24 4
gpt4 key购买 nike

我正在尝试按照使用 Facebook 登录的 Parse 教程进行操作。但是,示例代码与指南不符,所以那里的代码没有用。我完全按照指南操作,但在我登录后,它会将我定向到 Facebook 应用程序,我授予权限,它会返回到我正在构建的应用程序,但我收到以下错误

FBSDKLog:请求端点“我”时出错:必须为调用此端点指定一个打开的 FBSession。

这是怎么回事?在登录 Controller 中:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];


FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// handle successful response
} else if ([[[[error userInfo] objectForKey:@"error"] objectForKey:@"type"]
isEqualToString: @"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(@"The facebook session was invalidated");
[self logoutButtonAction:nil];
} else {
NSLog(@"Some other error: %@", error);
}
}];
if ([PFUser currentUser] && // Check if user is cached
[PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) { // Check if user is linked to Facebook
// Present the next view controller without animation
[self _presentUserDetailsViewControllerAnimated:NO];
}
}
- (IBAction)loginButtonTouchHandler:(id)sender {
// Set permissions required from the facebook user account
NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];
[PFFacebookUtils initializeFacebook];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
[_activityIndicator stopAnimating]; // Hide loading indicator

if (!user) {
NSString *errorMessage = nil;
if (!error) {
NSLog(@"Uh oh. The user cancelled the Facebook login.");
errorMessage = @"Uh oh. The user cancelled the Facebook login.";
} else {
NSLog(@"Uh oh. An error occurred: %@", error);
errorMessage = [error localizedDescription];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In Error"
message:errorMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Dismiss", nil];
[alert show];
} else {
if (user.isNew) {
NSLog(@"User with facebook signed up and logged in!");
} else {
NSLog(@"User with facebook logged in!");
}
[self _presentUserDetailsViewControllerAnimated:YES];
}
}];

[_activityIndicator startAnimating]; // Show loading indicator until login is finished
}

- (void)_presentUserDetailsViewControllerAnimated:(BOOL)animated {
UserDetailsViewController *detailsViewController = [[UserDetailsViewController alloc] init];
[self.navigationController pushViewController:detailsViewController animated:YES];
}

在我的 UserDetailsViewController 中:

- (void)viewDidLoad {
// ...
[self _loadData];
}

- (void)_loadData {
// ...
FBRequest *request = [FBRequest requestForMe];
[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 *facebookID = userData[@"id"];
NSString *name = userData[@"name"];
NSString *location = userData[@"location"][@"name"];
NSString *gender = userData[@"gender"];
NSString *birthday = userData[@"birthday"];
NSString *relationship = userData[@"relationship_status"];

NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];
// URL should point to https://graph.facebook.com/{facebookId}/picture?type=large&return_ssl_resources=1
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:pictureURL];

// Run network request asynchronously
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil && data != nil) {
// Set the image in the header imageView
self.headerImageView.image = [UIImage imageWithData:data];
}
}];

// Now add the data to the UI elements
// ...
}
}];

}

最佳答案

我们在尝试使用此功能创建自动登录功能时想通了:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// handle successful response
} else if ([[[[error userInfo] objectForKey:@"error"] objectForKey:@"type"]
isEqualToString: @"OAuthException"]) { // Since the request failed, we can check if it was due to an invalid session
NSLog(@"The facebook session was invalidated");
[self logoutButtonAction:nil];
} else {
NSLog(@"Some other error: %@", error);
}
}];
if ([PFUser currentUser] && // Check if user is cached
[PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) { // Check if user is linked to Facebook
// Present the next view controller without animation
[self _presentUserDetailsViewControllerAnimated:NO];
}
}

我们实际上最终跳过了 [PFFFacebookUtils initializeFacebook] 调用,因为它只会在您按下登录按钮时发生。解决方案是将此调用放入 appDelegate 方法 application:didFinishLaunchingWithOptions:

关于ios - FBSession 必须指定解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27762597/

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