2012-03-11 93 views
0

我正在开发一个iOs 5应用程序,并且我已经为iOs实施了Facebook SDK。我想和大家分享的图片让我的代码(不工作):我如何与Facebook SDK共享图片iOs

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];  

} 

所以它所有的作品,除非在共享图片中的一部​​分 - (无效)fbDidLogin。我该如何解决它?

+0

fbDidLogin中究竟发生了什么以及“错误”行为在哪里? – LordT 2012-03-11 17:29:25

+0

@LordT在fbDidLogin中,应用程序必须创建快照并将其上传到Facebook,但最后一部分不起作用。 – 2012-03-11 17:46:22

回答

2

你缺少必要的权限。根据https://developers.facebook.com/docs/reference/api/permissions/,您需要权限publish_stream。但是,即使有这样的许可,我并不是100%确定您可以发布给我/照片。尝试先张贴到饲料!

[编辑]事实证明我错了。要上传UIImage,您需要user_photos,publish_stream和upload_photos权限并发布给我/照片 - 您无法发布给我/提供上传图片的Feed。

直接来自fb的较新的facebook api explorer略胜一筹。 https://developers.facebook.com/tools/explorer

您可以在“发布”下查看文档here了解您可以发布的内容和位置。请注意,您只能将照片上传到现有相册,而您需要相册ID。

您应该在班级中实施FBRequestDelegate协议,以查看失败的原因和方式。

[edit 2]检查这个帖子:other SO post - 看起来你确实也需要publish_stream权限。再试一次,如果你无法绕过它,我会设置一个测试环境。

+0

我添加了publish_stream。所以,如果我不能张贴我/照片,我可以发布到哪里? – 2012-03-11 17:54:45

+0

我/ feed是新闻Feed – LordT 2012-03-11 17:59:05

+0

我已添加photo_upload权限。但它不起作用... – 2012-03-11 18:07:19

0

尝试添加访问令牌params中DIC

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"picture",message,@"caption",[facebook accesstoken],@"access_token",nil]; 
+0

它也不工作。有另一个想法? – 2012-03-11 17:25:11

+1

使用我/照片,而不是我/饲料..如果你想它作为饲料,你需要设置一个网址的图像不发送它作为对象。 – 2012-03-12 00:20:33