[英]Facebook Wall Post just after Facebook Signup iOS- facebook SDK 3.2
Facebook SDK 3.2 Facebook SDK 3.2
I need to post a wall post on facebook just after sign-in with faceboook. 使用faceboook登录后,我需要在Facebook上发布墙贴。
Here is the Code i am using: 这是我正在使用的代码:
Here i have a opened the Session For Facebook: 在这里,我打开了Facebook会话:
- (IBAction)FBSignup:(id)sender {
[[AppDelegate sharedObject] openSession:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
On State Change, I am populating Data with [self populateUserDetails] and then trying to post on facebook wall as well with [self postOnFB] but there is no post on facebook wall. 关于状态更改,我正在使用[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];
}
}
Here i am populting data with the facebook user object where i need to. 在这里,我在需要的地方使用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];
}
}];
}
}
Here i try to post the wall post with [self postOnFB] it gives error: 在这里,我尝试用[self postOnFB]发布墙帖,它给出了错误:
Terminating app due to uncaught exception 'com.facebook.sdk:InvalidOperationException', reason: 'FBSession: It is not valid to reauthorize while a previous reauthorize call has not yet completed.
由于未捕获的异常'com.facebook.sdk:InvalidOperationException'而终止应用程序,原因:'FBSession:在先前的重新授权调用尚未完成的情况下,重新授权无效。
Here i am posting to the wall. 我在这里贴墙。
- (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];
}
}
setting parameters for facebook wall post: 设置Facebook墙贴的参数:
- (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];
}
If i call postOnFB on some button action selector (after completion of data population through facebook user object), it post fine on the wall. 如果我在某个按钮动作选择器上调用postOnFB (通过Facebook用户对象完成数据填充后),它将在墙上发布良好。 But i need to post it just after i get the user object in startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary *user, NSError *error) method.
但是我需要在startWithCompletionHandler中获取用户对象后发布它:^(FBRequestConnection *连接,NSDictionary * user,NSError * error)方法。
Please help. 请帮忙。 Thank you everyone :)
谢谢大家 :)
Instead of opening session and then requesting publish permissions, directly ask for publish permissions to open the session 无需打开会话然后再请求发布权限,而是直接要求发布权限来打开会话
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:@"publish_stream",
nil] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session,
FBSessionState state, NSError *error) {
if (FBSession.activeSession.isOpen && !error) {
[self fbPostSettings];
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.