2014-12-06 58 views
3

我是iOS開發的新手。我想將圖像上傳到Facebook頁面。當圖像從Camera.Phen拿起來,我寫了一個代碼如下,但我得到一個錯誤「FBSession必須指定調用這個端點」。請給我解決方案。必須爲在iOS中調用此端點指定一個打開的FBSession

我的代碼等作爲

-(IBAction)cameraButtonPressed:(id)sender 
{ 
if (FBSession.activeSession.isOpen) 
{ 
    UIImage *uploadImage=[UIImage imageNamed:@"2.png"]; 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:accesstoken, @"CAACEdEose0cBAAFiJ51qowOOBfOcobm1E3v1ZBZCNYBSZAyspbQCXPUZCdZC620bRWYvChxGd96ZAOSMNyRZA9TYd9teQ9Q1m67wKx0n66YjTiYZBxczKIZB7t3tuLO5RYsNbZBfGweZCenMXRF7xOBE6DQ6450nmB0hRJzI8PrT18WZAfU878GhNryp5Vw4Up3jGZCwPWmuMxDZAoZAXVosyZBNKQ6i", 
          postingString, @"message",uploadImage,@"source", 
          nil]; 
    FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil 
                 graphPath:@"/me/feed" 
                 parameters:params 
                 HTTPMethod:@"POST"]; 
    FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init]; 
    [requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
    { 
     NSLog(@"facebook result >> %@", result); 
    }]; 
    [requestToPostConnection start]; 
} 
else 
{ 
    NSArray *permissions = [[NSArray alloc] initWithObjects: 
          @"user_likes", 
          @"read_stream", 
          @"publish_actions", 
          nil]; 
    FBSession *session = [[FBSession alloc] initWithPermissions:permissions]; 
    [FBSession setActiveSession:session]; 
} 
if([FBSession openActiveSessionWithAllowLoginUI:NO]) 
{ 
    UIImage *uploadImage=[UIImage imageNamed:@"2.png"]; 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:accesstoken, @"CAACEdEose0cBAAFiJ51qowOOBfOcobm1E3v1ZBZCNYBSZAyspbQCXPUZCdZC620bRWYvChxGd96ZAOSMNyRZA9TYd9teQ9Q1m67wKx0n66YjTiYZBxczKIZB7t3tuLO5RYsNbZBfGweZCenMXRF7xOBE6DQ6450nmB0hRJzI8PrT18WZAfU878GhNryp5Vw4Up3jGZCwPWmuMxDZAoZAXVosyZBNKQ6i", 
          postingString, @"message",uploadImage,@"source", 
          nil]; 
    FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil 
                 graphPath:@"/me/feed" 
                 parameters:params 
                 HTTPMethod:@"POST"]; 
    FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init]; 
    [requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
    { 
     NSLog(@"facebook result >> %@", result); 
    }]; 
    [requestToPostConnection start]; 

} 
else 
{ 
    NSLog(@"Nothing to DO BYE"); 
} 

這裏我的plist文件等作爲

<key>CFBundleURLTypes</key> 
<array> 
    <dict> 
     <key>CFBundleTypeRole</key> 
     <string>Editor</string> 
     <key>CFBundleURLName</key> 
     <string></string> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>fb74549074886xxxx</string> 
     </array> 
    </dict> 
</array> 
<key>CFBundleVersion</key> 
<string>1.0</string> 
<key>FacebookAppID</key> 
<string>7454907xxxxxx</string> 
<key>FacebookDisplayName</key> 
<string>Cele­b­r­a­t­e­C­h­r­i­s­tmas</string> 
+0

嗨,這是KARTHIK終於ü使用我的代碼,任何方式好,在這裏你的問題是檢查你的FBSession是否開放 – 2014-12-06 04:34:45

+0

@ Anbu.Karthik thnx bro最後你的代碼對我有用。但我不知道鋤頭檢查FBSession是否開放。但我可以從R&D做到。然後在這裏回覆你任何問題都會發生o不是。 – 2014-12-06 04:38:58

+0

k顯示您的完整錯誤代碼... – 2014-12-06 04:42:26

回答

1
-(IBAction)cameraButtonPressed:(id)sender 
{ 
    // here add your image did finish method 

    if ([[FBSession activeSession] isOpen]) { 
    /* 
    * if the current session has no publish permission we need to reauthorize 
    */ 
    if ([[[FBSession activeSession] permissions]indexOfObject:@"publish_stream"] == NSNotFound) { 

     [[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObjects: @"publish_stream",nil] defaultAudience:FBSessionDefaultAudienceFriends 
               completionHandler:^(FBSession *session,NSError *error){ 
                [self postData]; 
               }]; 

    }else{ 
     [self postData]; 
    } 
}else{ 

    /* 
    * open a new session with publish permission 
    */ 

    [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects: @"publish_stream", nil] 
             defaultAudience:FBSessionDefaultAudienceFriends 
              allowLoginUI:NO 
            completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { 
             if (!error && status == FBSessionStateOpen) { 
              [self postData]; 
             }else{ 
              NSLog(@"error"); 
             } 
            }]; 
} 


} 



-(void)postData 
{ 
UIImage *uploadImage=[UIImage imageNamed:@"2.png"]; 
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:accesstoken, @"CAACEdEose0cBAAFiJ51qowOOBfOcobm1E3v1ZBZCNYBSZAyspbQCXPUZCdZC620bRWYvChxGd96ZAOSMNyRZA9TYd9teQ9Q1m67wKx0n66YjTiYZBxczKIZB7t3tuLO5RYsNbZBfGweZCenMXRF7xOBE6DQ6450nmB0hRJzI8PrT18WZAfU878GhNryp5Vw4Up3jGZCwPWmuMxDZAoZAXVosyZBNKQ6i", 
         postingString, @"message",uploadImage,@"source", 
         nil]; 
FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil 
                graphPath:@"/me/feed" 
                parameters:params 
                HTTPMethod:@"POST"]; 
FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init]; 
[requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
{ 
    NSLog(@"facebook result >> %@", result); 
}]; 
[requestToPostConnection start]; 
} 
+0

當我在我的例子中寫你的代碼,然後沒有發生。只有相機按鈕按下。如果循環沒有調用。 – 2014-12-06 05:26:40

+0

它也是Print'com.facebook.sdk:InvalidOperationException',原因:'FBSession:在日誌錯誤中嘗試對未打開的會話重新授權權限'。 – 2014-12-06 05:32:44

+0

然後首先打開sessioion' – 2014-12-06 05:50:15

相關問題