脸书 SDK 3.2
在使用 Facebook 登录后,我需要在 facebook 上发布一个墙贴。
这是我正在使用的代码:
在这里,我打开了 Facebook 会话:
- (IBAction)FBSignup:(id)sender {
[[AppDelegate sharedObject] openSession:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
在状态更改时,我使用[self populateUserDetails] 填充数据,然后尝试使用[self postOnFB]在 facebook 墙上发帖,但 facebook 墙上没有帖子。
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
[self populateUserDetails];
[self postOnFB];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:SCSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
在这里,我在需要的地方使用 facebook 用户对象填充数据。
- (void)populateUserDetails
{
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
///////////////////////////////
// Populating Data Where Needed
///////////////////////////////
// [self postOnFB];
}
}];
}
}
在这里,我尝试使用[self postOnFB]发布墙帖,但出现错误:
由于未捕获的异常“com.facebook.sdk:InvalidOperationException”而终止应用程序,原因:“FBSession:在先前的重新授权调用尚未完成时重新授权无效。
我在这里张贴到墙上。
- (void)postOnFB{
// Ask for publish_actions permissions in context
if ([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
[self fbPostSettings];
}
}];
} else {
// If permissions present, publish the story
[self fbPostSettings];
}
}
脸书墙贴的设置参数:
- (void)fbPostSettings{
NSDictionary * facebookParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
kFacebookPostLink, @"link",
kFacebookPostImage, @"picture",
kFacebookPostName, @"name",
kFacebookPostCaption, @"caption",
kFacebookPostDescription, @"description",
nil];
[FBRequestConnection startWithGraphPath:@"me/feed"
parameters:facebookParams
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
@"error: domain = %@, code = %d",
error.domain, error.code];
} else {
alertText = [NSString stringWithFormat:
@"Posted action, id: %@",
[result objectForKey:@"id"]];
}
NSLog(@"%@",alertText);
}];
}
- (void)sessionStateChanged:(NSNotification*)notification {
[self populateUserDetails];
}
如果我在某个按钮操作选择器上调用postOnFB(通过 facebook 用户对象完成数据填充后),它会在墙上正常发布。但我需要在startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary *user, NSError *error)方法中获取用户对象后发布它。
请帮忙。谢谢大家 :)