[英]Post to Facebook from iOS App using FacebookSDK
I am using FacebookSDK in one of my applications to Login to the app and Share something. 我在其中一个应用程序中使用FacebookSDK登录该应用程序并共享一些东西。 The login is completed and i can now Login to the application using this SDK. 登录已完成,我现在可以使用此SDK登录到应用程序。 Below is the code I am using for login. 以下是我用于登录的代码。
-(void)didSelectLoginWithFB
{
if (FBSession.activeSession.isOpen) {
[appDelegate closeSession];
}
else {
[appDelegate openSessionWithAllowLoginUI:YES];
}
if(FBSession.activeSession.accessToken ){
[self performSelector:@selector(FBLoginAction) withObject:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(FBLoginDidFinish:)
name:@"FBLoginDidFinish"
object:appDelegate];
}
-(void)FBLoginDidFinish:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self performSelector:@selector(FBLoginAction) withObject:nil];
}
-(void)FBLoginAction
{
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSDictionary *userInfo = (NSDictionary *)user;
NSLog(@"User dic %@",userInfo);
// fbUserId = user.id;
// NSLog(@"User dic %@",fbUserId);
userMail = [userInfo objectForKey:@"email"];
NSLog(@"User E Mail --- %@", userMail);
loginFlag = 200;
[self getDataFromServer:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Facebook Connection Error !!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}];
}
}
Now, I want to post something to the User's facebook wall (timeline). 现在,我想在用户的Facebook墙上(时间线)发布一些内容。 How can I do this using this SDK ? 如何使用此SDK执行此操作? I didn't get any info from developer.facebook.com for this sdk. 我没有从developer.facebook.com获得此sdk的任何信息。 Please Help ! 请帮忙 !
I know this is very late, but I hope it'll help.. 我知道这已经很晚了,但希望对您有所帮助。
You can use following code to post on user's wall/timeline- 您可以使用以下代码在用户的背景墙/时间线上发布-
- (void)postToFacebook{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (appDelegate.session.state != FBSessionStateCreated || !appDelegate.session.isOpen){
// Create a new, logged out session.
NSArray *permissions = [NSArray arrayWithObject:@"publish_actions, publish_stream"];
appDelegate.session = [[FBSession alloc] initWithPermissions:permissions];
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (appDelegate.session.state!=258 && appDelegate.session.state!=257) {
[self postOpenGraphAction];
}
}];
}else{
if (appDelegate.session.isOpen) {
[self postOpenGraphAction];
}
}
}
- (void)postOpenGraphAction {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"", @"picture", @"", @"name", @"", @"link", @"", @"caption", @"", @"description", @"Your message to post", @"message", nil];
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[FBSession setActiveSession:appDelegate.session];
[FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error) {
NSLog(@"Error on publishing story: %@", error);
} else {
NSLog(@"Published on wall successfully");
}
}];
}
I have used session object created in my AppDelegate, instead of FBSession activeSession. 我使用了在我的AppDelegate中创建的会话对象,而不是FBSession activeSession。 You can use as per your code. 您可以根据自己的代码使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.