2013-03-16 68 views
0

我在我的一個應用程序中使用FacebookSDK登錄到應用程序並共享一些內容。登錄完成後,我現在可以使用此SDK登錄到應用程序。以下是我用於登錄的代碼。使用FacebookSDK從iOS應用程序發佈到Facebook

-(void)didSelectLoginWithFB 
{ 
    if (FBSession.activeSession.isOpen) { 
     [appDelegate closeSession]; 
    } 
    else { 
     [appDelegate openSessionWithAllowLoginUI:YES]; 
    } 

    if(FBSession.activeSession.accessToken){ 

     [self performSelector:@selector(FBLoginAction) withObject:nil]; 
    } 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(FBLoginDidFinish:) 
               name:@"FBLoginDidFinish" 
               object:appDelegate]; 

} 

-(void)FBLoginDidFinish:(NSNotification *)notification 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [self performSelector:@selector(FBLoginAction) withObject:nil]; 

    } 

-(void)FBLoginAction 
{ 
    if (FBSession.activeSession.isOpen) { 

    [[FBRequest requestForMe] startWithCompletionHandler: 
     ^(FBRequestConnection *connection, 
      NSDictionary<FBGraphUser> *user, 

      NSError *error) { 
      if (!error) { 

       NSDictionary *userInfo = (NSDictionary *)user; 
       NSLog(@"User dic %@",userInfo); 
//     fbUserId = user.id; 
//     NSLog(@"User dic %@",fbUserId); 

       userMail = [userInfo objectForKey:@"email"]; 
       NSLog(@"User E Mail --- %@", userMail); 
        loginFlag = 200; 
       [self getDataFromServer:nil]; 
      } 

      else{ 

       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Facebook Connection Error !!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ]; 
       [alert show]; 
      } 

     }]; 



    } 

} 

現在,我想發佈一些東西到用戶的facebook牆(時間線)。我如何使用此SDK來做到這一點?我沒有從這個sdk的developer.facebook.com獲得任何信息。 請幫忙!

回答

3

我知道這是非常晚了,但我希望它會幫助..

您可以使用下面的代碼來發布用戶的牆上創建/ timeline-

- (void)postToFacebook{ 

    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

    if (appDelegate.session.state != FBSessionStateCreated || !appDelegate.session.isOpen){ 

     // Create a new, logged out session. 
     NSArray *permissions = [NSArray arrayWithObject:@"publish_actions, publish_stream"]; 
     appDelegate.session = [[FBSession alloc] initWithPermissions:permissions]; 

     // if the session isn't open, let's open it now and present the login UX to the user 
     [appDelegate.session openWithCompletionHandler:^(FBSession *session, 
                 FBSessionState status, 
                 NSError *error) { 
      if (appDelegate.session.state!=258 && appDelegate.session.state!=257) { 

       [self postOpenGraphAction]; 
      } 
     }]; 
    }else{ 
     if (appDelegate.session.isOpen) { 
      [self postOpenGraphAction]; 
     } 
    } 
} 

- (void)postOpenGraphAction { 

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"", @"picture", @"", @"name", @"", @"link", @"", @"caption", @"", @"description", @"Your message to post", @"message", nil]; 

    AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate; 
    [FBSession setActiveSession:appDelegate.session]; 

    [FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST" 
          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
           if (error) { 
            NSLog(@"Error on publishing story: %@", error); 
           } else { 
                  NSLog(@"Published on wall successfully"); 
           } 
          }]; 
} 

我已經使用會話對象在我的AppDelegate中,而不是FBSession的activeSession。 您可以按照您的代碼使用。

相關問題