在我的应用程序中我使用Facebook sdk 3.1并使用它我尝试做墙贴.使用此代码片段,我可以在Friend Wall和我自己的墙上成功发布图像和消息.我只需更改我/照片和 friendId /照片.
-(IBAction)postPhoto:(id)sender { UIImage *image=[UIImage imageNamed:@"baby.jpg"]; NSArray *frndArray=[[NSArray alloc] initWithObjects:@"Friend ID", nil]; [self postImageOnFriendsWall:image FriendsArray:frndArray]; } -(void)postImageOnFriendsWall:(UIImage*)image FriendsArray:(NSArray*)friends { AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; // if we don't have permission to announce, let's first address that if ([appDelegate.session.permissions indexOfObject:@"publish_actions"] == NSNotFound) { NSArray *permissions =[NSArray arrayWithObjects:@"publish_actions",@"publish_stream",@"manage_friendlists", nil]; [FBSession setActiveSession:appDelegate.session]; [[FBSession activeSession] reauthorizeWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) { if (!error) { // have the permission [self postImageOnFriendsWall:image FriendsArray:friends]; } else { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } }]; } else { //post image for (iduser in friends) { NSString* szMessage = @"Check out!"; NSString *userID = user; NSLog(@"trying to post image of %@ wall",userID); NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init]; [postVariablesDictionary setObject:UIImagePNGRepresentation(image) forKey:@"picture"]; [postVariablesDictionary setObject:szMessage forKey:@"message"]; [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/photos",userID] parameters:postVariablesDictionary HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { if (error) { //showing an alert for failure UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Facebook" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else { //showing an alert for success UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Facebook" message:@"Shared the photo successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } }]; } } }
对于留言墙帖我使用了我/ feed及其工作,但对于朋友friendID/feed会给我错误:HTTP状态代码:403和在警告框中我得到了com.facebook.sdk错误5.
所以,请指导我,我做错了什么?,或者我必须为墙贴做些什么.
这是解决方案,
我将我的FB SDK 3.1更新为3.2然后导入
#import
然后更新
[[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {}];
代替
[[FBSession activeSession] reauthorizeWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error){}];
并更新
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"FRIEND ID", @"to", @"text change", @"caption", @"this is test message on ur wall", @"description", @"https://website.com/share", @"link", @"http://website.com/iossdk_logo.png", @"picture", nil]; [FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler: ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {}];
代替
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/photos",userID] parameters:postVariablesDictionary HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error){}];
我得到了对话框,点击分享成功发布在FRIEND的墙上.
希望这段代码可以帮助别人.