2012-03-11 91 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