我正在开发iOs 5应用程序,并且我已经为iOs实施了Facebook SDK。我想分享一张图片,以便我的代码(不能正常工作):
if (buttonIndex==3) {
if (facebook == nil)
{
// Setup Facebook connection
[self setUpFacebookConnection];
}else {
[self fbDidLogin];
}
}
}
- (void)setUpFacebookConnection
{
facebook = [[Facebook alloc] initWithAppId:@"245697495524266" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid])
{
NSArray* permissions = [[NSArray alloc] initWithObjects:
@"offline_access", @"publish_stream", @"publish_actions", @"photo_upload", nil];
[facebook authorize:permissions];
}
}
- (void)fbDidLogin{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
bannerview2.hidden=YES;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
// retrieve the current graphics context
CGContextRef context = UIGraphicsGetCurrentContext();
// render view into context
[self.view.layer renderInContext:context];
// create image from context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// save image to photo album
UIImageWriteToSavedPhotosAlbum(image,
self,
nil,
@"image.png");
bannerview2.hidden=NO;
NSMutableString *message = [[NSMutableString alloc] init];
[NSString stringWithFormat:@"Resultado final %@: %d vs. %@: %d. Mi jugador hizo %d puntos y cometió %d faltas.",intnomlocal, puntsl, intnomvisitant, puntsv, puntsjugadorint, faltesjugadorint];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"picture", message,@"message", nil];
[facebook requestWithGraphPath:@"me/feed"andParams:params andHttpMethod:@"POST" andDelegate:self];
}
所以这一切都有效,除非共享图片的部分 - (void)fbDidLogin。我该如何解决?
答案 0 :(得分:2)
您缺少必要的权限。根据{{3}},您需要权限publish_stream。但是,即使有了这个许可,我也不能100%确定你可以发给我/照片。请先尝试发布到Feed!
[编辑]结果我错了。要上传UIImage,您需要user_photos,publish_stream和upload_photos权限并发布给我/照片 - 您无法向我发送/提供要上传的图片。
更新的facebook api explorer,直接来自fb,好一点。 https://developers.facebook.com/docs/reference/api/permissions/
您可以查看“发布”下的文档https://developers.facebook.com/tools/explorer,了解您可以发布的内容和位置。请注意,您只能将照片上传到您需要相册ID的现有相册。
您应该在班级中实施here协议,以了解失败的原因和方法。
[编辑2]查看此帖子:FBRequestDelegate - 您似乎确实需要publish_stream权限。再试一次,如果你无法绕过它,我会设置一个测试环境。
答案 1 :(得分:0)
尝试在params dic
中添加访问令牌 NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"picture",message,@"caption",[facebook accesstoken],@"access_token",nil];