2012-12-12 93 views
1

我正在進行Facebook集成並嘗試使用發佈Feed功能進行單一登錄。
我正在使用最新的FacebookSDK。我有Facebook的Hackbook示例代碼,但是,我對這一切都很陌生,因此很難完全理解所有這些事情。在Facebook(iOS)中使用SSO發佈Feed

雖然搜索SSO我有一些代碼,它工作正常。這裏是我使用的代碼(在本頁面的結尾有一個附源代碼)

FBUtils.h and FBUtils.m class

ViewController.m

- (IBAction)publishFeed:(id)sender { 

//For SSO 

[[FBUtils sharedFBUtils] initializeWithAppID:@"3804765878798776"]; 
NSArray *permision = [NSArray arrayWithObjects:@"read_stream",@"publish_stream", nil]; 
[[FBUtils sharedFBUtils] LoginWithPermisions:permision]; 
[FBUtils sharedFBUtils].delegate = self; 
FBSBJSON *jsonWriter = [FBSBJSON new]; 


/// for publishfeed 

    NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys: 
                @"Get Started",@"name",@"https://itunes.apple.com?ls=1&mt=8",@"link", nil], nil]; 
    NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks]; 
    // Dialog parameters 
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
           @"I have lot of fun preparing.", @"name", 
           @" exam", @"caption", 
           @" ", @"description", 
           @"https://itunes.apple.com", @"link", 
           @"http://mypng", @"picture", 
           actionLinksStr, @"actions", 
           nil]; 

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [[delegate facebook] dialog:@"feed" 
         andParams:params 
        andDelegate:self]; 

當我點擊我的應用程序的Facebook按鈕它將我重定向到Facebook,然後重新回到我的應用程序。現在,我想要的是在返回到應用程序後觸發publishFeed事件,並且它應該直接向用戶請求發佈或取消選項。但它是這樣要求再次登錄。

enter image description here

任何一個可以幫我在這或請建議我的正確途徑。
您的建議將是一個很大的幫助。

回答

0

在您的方法中,您不檢查應用程序是否有權發佈帖子,以及用戶是否先前登錄過。所以,每次你調用這個方法,應用程序都希望你登錄。我認爲這是問題。

如果我是對的,您需要在您的方法中添加權限和登錄控制。這是來自另一個項目的示例代碼,您可以獲取它背後的邏輯。

- (IBAction)facebookShare:(id)sender 
{ 

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

    // You check the active session here. 
    if (FBSession.activeSession.isOpen) 
    { 
      // You check the permissions here. 
      if ([FBSession.activeSession.permissions 
      indexOfObject:@"publish_actions"] == NSNotFound) { 
      // No permissions found in session, ask for it 
      [FBSession.activeSession 
      reauthorizeWithPublishPermissions: 
      [NSArray arrayWithObject:@"publish_actions"] 
      defaultAudience:FBSessionDefaultAudienceFriends 
      completionHandler:^(FBSession *session, NSError *error) { 
       if (!error) { 
        // If permissions granted, publish the story 
        [self postFacebook]; 

        [[[UIAlertView alloc] initWithTitle:@"Result" 
               message:@"Posted in your wall." 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil] 
         show]; 
       } 
      }]; 
     } else { 
      // If permissions present, publish the story 
      [self postFacebook]; // ------> This is your post method. 

      [[[UIAlertView alloc] initWithTitle:@"Sonuç" 
             message:@"Duvarında paylaşıldı." 
             delegate:self 
           cancelButtonTitle:@"Tamam" 
           otherButtonTitles:nil] 
      show]; 
     } 
    } 

    else 
    { 
    // If there is no session, ask for it. 
    [appDelegate openSessionWithAllowLoginUI:YES]; 
    } 


    // NSLog(@"Post complete."); 

}