1

我正在使用此代码在 Facebook 新闻源上分享一个项目:

-(void)recommendOnFacebook:(Item *)currentItem{    
if(!facebook){
    facebook = [[Facebook alloc] initWithAppId:@"myappid" andDelegate:self];
} 
NSMutableDictionary *params2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                [NSString stringWithFormat:@"%@", currentItem.name], @"name",
                                [shop name], @"caption",
                                currentItem.description, @"description",
                                [NSString stringWithFormat:@""], @"link",
                                currentItem.imagePath, @"picture",
                                nil, @"actions",
                                nil];    
[facebook dialog:@"feed" 
     andParams:params2
     andDelegate:self];
}

如果我已登录,我可以成功看到共享提要的对话框。但是,如果我没有登录,则在此代码块完成后,api 对话框会询问我的凭据。我需要的是,如果用户未登录,api显示对话框,用户成功登录后,显示另一个对话框以继续共享进度。

4

1 回答 1

2

你需要看看 FBSessionDelegate。这是我的分享代码

- (IBAction) facebookShare:(id)sender {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    if (![delegate.facebook isSessionValid]) {
        [delegate.facebook authorize:[NSArray arrayWithObjects:@"publish_stream", @"offline_access", nil]];
    }
    else {
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
         [NSString stringWithFormat:@"TEXT", score.text], @"name",
         @"", @"caption",
         @"DESC.", @"description",
         @"link", @"link",
         @"imagelink", @"picture",
         nil];  
        [delegate.facebook dialog:@"feed"
               andParams:params
             andDelegate:self];
    }

}

#pragma mark - FB Session Delegate

- (void)fbDidLogin {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    /*NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[delegate.facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[delegate.facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];*/
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
         [NSString stringWithFormat:@"TEXT", score.text], @"name",
         @"", @"caption",
         @"DESC.", @"description",
         @"link", @"link",
         @"imagelink", @"picture",
         nil];  
        [delegate.facebook dialog:@"feed"
               andParams:params
             andDelegate:self];
}

编辑

对于公共咖啡馆的注销,您需要确保您永远不会像在 FB 会话委托方法 fbDidLogin 中那样在 NSUSerDefaults 中保存访问令牌

现在实现 FBDialogDelegate 方法

- (void)dialogDidComplete:(FBDialog *)dialog {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.facebook logout:self];
}
于 2012-05-18T11:37:12.097 回答