如何在IOS中登录Facebook并分享新闻Feed?

时间:2012-05-18 11:26:35

标签: ios facebook

我正在使用此代码在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];
}

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

1 个答案:

答案 0 :(得分: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];
}

修改

要在公共咖啡馆的情况下注销,您需要确保不要像在NS会话委托方法中那样在NSUSerDefaults中保存访问令牌fbDidLogin

现在实现FBDialogDelegate方法

- (void)dialogDidComplete:(FBDialog *)dialog {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.facebook logout:self];
}