我正在开发Facebook集成并尝试使用发布Feed功能进行单点登录 我正在使用最新的FacebookSDK。我有Facebook的Hackbook示例代码但是,我是所有这些的新手所以很难完全理解所有这些。
在搜索SSO时我得到了一些代码,它工作正常。这是我正在使用的代码(在本页末尾附有源代码)
ViewController.m
- (IBAction)publishFeed:(id)sender {
//For SSO
[[FBUtils sharedFBUtils] initializeWithAppID:@"3804765878798776"];
NSArray *permision = [NSArray arrayWithObjects:@"read_stream",@"publish_stream", nil];
[[FBUtils sharedFBUtils] LoginWithPermisions:permision];
[FBUtils sharedFBUtils].delegate = self;
FBSBJSON *jsonWriter = [FBSBJSON new];
/// for publishfeed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
@"Get Started",@"name",@"https://itunes.apple.com?ls=1&mt=8",@"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
// Dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"I have lot of fun preparing.", @"name",
@" exam", @"caption",
@" ", @"description",
@"https://itunes.apple.com", @"link",
@"http://mypng", @"picture",
actionLinksStr, @"actions",
nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] dialog:@"feed"
andParams:params
andDelegate:self];
当我点击我的应用程序中的Facebook按钮时,它会将我重定向到Facebook,然后重新调回我的应用程序。现在,我想要的是在返回应用程序后立即触发publishFeed事件,它应该直接向用户询问帖子或取消选项。但它要求再次登录这样。
任何人都可以帮助我,或者请以正确的方式建议我 您的建议将是一个很大的帮助。
答案 0 :(得分:0)
在您的方法中,您不会检查应用是否有权发布帖子以及用户之前是否已登录。因此,每次调用此方法时,应用程序都希望您登录。我认为这就是问题。
如果我是对的,您需要在此方法中添加权限和登录控制。这是我的另一个项目的示例代码,你可以得到它背后的逻辑。
- (IBAction)facebookShare:(id)sender
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// You check the active session here.
if (FBSession.activeSession.isOpen)
{
// You check the permissions here.
if ([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self postFacebook];
[[[UIAlertView alloc] initWithTitle:@"Result"
message:@"Posted in your wall."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
show];
}
}];
} else {
// If permissions present, publish the story
[self postFacebook]; // ------> This is your post method.
[[[UIAlertView alloc] initWithTitle:@"Sonuç"
message:@"Duvarında paylaşıldı."
delegate:self
cancelButtonTitle:@"Tamam"
otherButtonTitles:nil]
show];
}
}
else
{
// If there is no session, ask for it.
[appDelegate openSessionWithAllowLoginUI:YES];
}
// NSLog(@"Post complete.");
}