0

我正在尝试使用 Facebook SDK 发布状态。一些用户已经使用 Facebook 登录。所以我有这个代码:

 if (FBSession.activeSession.isOpen) {


    NSLog(@"Already Open%@",[[FBSession activeSession] accessTokenData].accessToken);

   // NSString *tok = [[FBSession activeSession] accessTokenData].accessToken;

    NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];

    [[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends
                                               completionHandler:^(FBSession *session,NSError *error) {
                                                   if(!error){
                                                       NSLog(@"Publish Permission Granted");

                                                   }
                                                   else
                                                   {
                                                       NSLog(@"Publish to get Read Permission");
                                                   } }];



    //Remove indicator
    [_activityView removeFromSuperview];


} else {


    // OPEN Session!
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) {
                                      // if login fails for any reason, we alert
                                      if (error) {

                                          // show error to user.

                                      } else if (FB_ISSESSIONOPENWITHSTATE(status)) {

                                          // no error, so we proceed with requesting user details of current facebook session.

                                          NSLog(@"----%@",[session accessTokenData].accessToken);

                                          //NSString *tok = [session accessTokenData].accessToken;

                                          NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];

                                          [[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends
                                                                                completionHandler:^(FBSession *session,NSError *error) {
                                                                                    if(!error){
                                                                                        NSLog(@"Publish Permission Granted");
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        NSLog(@"Publish to get Read Permission");
                                                                                    } }];


                                          [_activityView removeFromSuperview];

                                          // [self promptUserWithAccountName];   // a custom method - see below:
                                      }
                                  }];  
}

因此,让我们关注第一部分,即假设用户打开了会话(第二个只是打开一个新的会话,以防没有可用的会话)。授予发布权限后,我将如何发布带有 url 和图片的状态?Facebook 的例子根本没有帮助。我发现了一些其他的例子,但大多数都已经过时了。

我设法发布了一个简单的帖子:

FBRequest *postRequest = [FBRequest requestForPostStatusUpdate:@"hi" ];

                                                       [postRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {


                                                           // TODO: Check for success / failure here

                                                       }];

经过盛大的发布许可。我想要与 url 描述 imgurl 和标题类似的东西。

4

1 回答 1

1

在我的 ibaction 方法中,我就是这样做的:

分享图片:

UIImage *img = myImage; 
FBLoginView *loginview = [[FBLoginView alloc] init];
loginview.delegate = self;

[self performPublishAction:^{

    [FBRequestConnection startForUploadPhoto:img
                           completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                               [self showAlert:@"Photo Post" result:result error:error];
                           }];

}];

分享网址:

NSURL *urlToShare = [NSURL URLWithString:@"http://developers.facebook.com/ios"];

FBAppCall *appCall = [FBDialogs presentShareDialogWithLink:urlToShare
                                                          name:@"Hello Facebook"
                                                       caption:nil
                                                   description:@"The 'Hello Facebook' sample application showcases simple Facebook integration."
                                                       picture:nil
                                                   clientState:nil
                                                       handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                           if (error) {
                                                               NSLog(@"Error: %@", error.description);
                                                           } else {
                                                               NSLog(@"Success!");
                                                           }
                                                       }];

也添加这些:

- (void) performPublishAction:(void (^)(void)) action
{
    if([[FBSession activeSession]isOpen])
    {
        if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
            // if we don't already have the permission, then we request it now
            [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
                                                  defaultAudience:FBSessionDefaultAudienceFriends
                                                completionHandler:^(FBSession *session, NSError *error) {
                                                    if (!error) {
                                                        action();
                                                    }
                                                    //For this example, ignore errors (such as if user cancels).
                                                }];
        } else {
            action();
        }
    }
    else
    {       
        [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                           defaultAudience:FBSessionDefaultAudienceFriends
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                             if (!error && status == FBSessionStateOpen) {

                                             }else{
                                                 NSLog(@"Session error");
                                                 [self fbResync];
                                                 [NSThread sleepForTimeInterval:0.5];   //half a second
                                                 [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                                                                    defaultAudience:FBSessionDefaultAudienceFriends
                                                                                       allowLoginUI:YES
                                                                                  completionHandler:^(FBSession *session, FBSessionState status, NSError *error){

                                                                                  }];
                                             }
                                         }];
    }
}

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{

}

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user
{
    self.loggedInUser = user;
}

- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
    self.loggedInUser = nil;
}

-(void)fbResync
{
    ACAccountStore *accountStore;
    ACAccountType *accountTypeFB;
    if ((accountStore = [[ACAccountStore alloc] init]) && (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){

        NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
        id account;
        if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])){

            [accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
                //we don't actually need to inspect renewResult or error.
                if (error){

                }
            }];
        }
    }
}

希望这会有所帮助...查看 Facebook SDK 示例中的 HelloFacebookSample。

尝试这个:

[self performPublishAction:^{
                NSString *message = [NSString stringWithFormat:@"Updating status for %@ at %@", self.loggedInUser.first_name, [NSDate date]];

                [FBRequestConnection startForPostStatusUpdate:message
                                            completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                                                [self showAlert:message result:result error:error];
                                                self.buttonPostStatus.enabled = YES;
                                            }];

                self.buttonPostStatus.enabled = NO;
            }];
于 2013-07-28T11:26:16.070 回答