gpt4 book ai didi

ios - 使用自定义登录按钮检索 Facebook 页面喜欢

转载 作者:行者123 更新时间:2023-11-29 12:26:50 25 4
gpt4 key购买 nike

我已经下载了这个用于使用 Facebook 自定义登录的示例项目:

FBLoginCustomUISample

作为测试,我想实现在 facebook sdk 上找到的这个方法检索用户喜欢的页面:

/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/likes"
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];

我已将其复制粘贴到 CustomLoginViewController.m 中,如下所示:

- (IBAction)buttonTouched:(id)sender
{
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];

// If the session state is not any of the two "open" states when the button is clicked
} else {
// Open a session showing the user the login UI
// You must ALWAYS ask for public_profile permissions when opening a session
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {

// Retrieve the app delegate
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[appDelegate sessionStateChanged:session state:state error:error];
[FBRequestConnection startWithGraphPath:@"/me/likes?limit=10"
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];
}];
}

}

当我登录时返回一个空数据。

在使用 facebook 提供的示例中的方法后,我尝试在我的 SignUpSetProfileDetailsViewController.m 类中实现下一个代码:

- (void)facebookButtonFunction{


// Open a session showing the user the login UI
// You must ALWAYS ask for public_profile permissions when opening a session
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"public_profile",
@"user_interests ",
nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {

[FBRequestConnection startWithGraphPath:@"/me/interests"
parameters:nil
HTTPMethod:@"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(@"%@",result);
}];
// Retrieve the app delegate

// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[self sessionStateChanged:session state:state error:error];
}];
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(@"Session opened");
// Show the user the logged-in UI
[self userLoggedIn];
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(@"Session closed");
// Show the user the logged-out UI
[self userLoggedOut];
}

// Handle errors
if (error){
NSLog(@"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = @"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
[self showMessage:alertText withTitle:alertTitle];
} else {

// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"User cancelled login");

// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = @"Session Error";
alertText = @"Your current session is no longer valid. Please log in again.";
[self showMessage:alertText withTitle:alertTitle];

// For simplicity, here we just show a generic message for all other errors
// You can learn how to handle other errors using our guide: https://developers.facebook.com/docs/ios/errors
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];

// Show the user an error message
alertTitle = @"Something went wrong";
alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
[self showMessage:alertText withTitle:alertTitle];
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
[self userLoggedOut];
}
}
- (void)userLoggedOut
{
// Set the button title as "Log in with Facebook"
[facebookButtonLabel setText:@"Connect"];
[facebookButtonLabel setTextColor:[UIColor whiteColor]];
}

// Show the user the logged-in UI
- (void)userLoggedIn
{
// Set the button title as "Log out"
[facebookButtonLabel setText:@"Connected"];
[facebookButtonLabel setTextColor:[UIColor yellowColor]];

// Welcome message
[self showMessage:@"You're now logged in with Facebook!" withTitle:@"Welcome!"];

}

- (void)showMessage:(NSString *)text withTitle:(NSString *)title
{
[[[UIAlertView alloc] initWithTitle:title
message:text
delegate:self
cancelButtonTitle:@"OK!"
otherButtonTitles:nil] show];
}

以编程方式添加按钮的地方,我所做的是丢失 appdelegate 实现

最佳答案

将user_likes添加到权限

 NSArray *permissions = [[NSArray alloc] initWithObjects:
@"public_profile",
@"user_likes",
nil];

尽管单独管理权限列表会更好,因为您需要管理大量权限(读取和写入)以获取各种数据

关于ios - 使用自定义登录按钮检索 Facebook 页面喜欢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28810016/

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