如何在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];
}

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

你需要看看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];
}

编辑

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

现在实现 FBDialogDelegate 方法

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

最新更新