如何在 Facebook 上分享帖子时解决登录问题

Posted

技术标签:

【中文标题】如何在 Facebook 上分享帖子时解决登录问题【英文标题】:How to solve login issue while sharing post on Facebook 【发布时间】:2012-12-28 10:37:51 【问题描述】:

我想在 facebook 上分享一个帖子,当我使用 ios 6 时它可以正常工作,但在 iOS 5 和 iOS 5.1 中,它会出现登录问题,当我登录并转到已经授权的页面并按下确定按钮时,它会将我拉回来像这样登录并在这两个页面上一次又一次地重复

当我按下确定时它会返回

 case 1: // facebook
        
            if(NSClassFromString(@"SLComposeViewController"))
            
                SLComposeViewController *fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
                SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result)

                    [fbController dismissViewControllerAnimated:YES completion:nil];

                    switch(result)
                        case SLComposeViewControllerResultCancelled:
                        default:
                        
                            NSLog(@"Cancelled.....");

                        
                            break;
                        case SLComposeViewControllerResultDone:
                        
                            ALERT_VIEW(@"Successfully posted to facebook.");
                        
                            break;
                    ;

                [fbController setInitialText:quotesss];
                [fbController setCompletionHandler:completionHandler];
                [self presentViewController:fbController animated:YES completion:nil];
            
            else
            
                DEFacebookComposeViewControllerCompletionHandler completionHandler = ^(DEFacebookComposeViewControllerResult result) 
                    switch (result) 
                        case DEFacebookComposeViewControllerResultCancelled:
                            NSLog(@"Facebook Result: Cancelled");
                            break;
                        case DEFacebookComposeViewControllerResultDone:
                            ALERT_VIEW(@"Successfully posted to facebook.");
                            break;
                    

                    [self dismissViewControllerAnimated:YES completion:nil];
                ;
                DEFacebookComposeViewController *facebookViewComposer = [[DEFacebookComposeViewController alloc] init];
                self.modalPresentationStyle = UIModalPresentationCurrentContext;

                [facebookViewComposer setInitialText:quotesss];

                facebookViewComposer.completionHandler = completionHandler;
                [self presentViewController:facebookViewComposer animated:YES completion:nil];
                [facebookViewComposer release];
            
            break;
        
        case 2: // twitter
        
            NSString *text = [NSString stringWithFormat:@"%@", quotesss ];
            if([text length] > 135)
            
                text = [NSString stringWithFormat:@"%@%@", quotesss ,@"..."];
                text = [text substringToIndex:135];

            

            if(NSClassFromString(@"SLComposeViewController"))
            
                SLComposeViewController *twitterController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
                SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result)

                    [twitterController dismissViewControllerAnimated:YES completion:nil];

                    switch(result)
                        case SLComposeViewControllerResultCancelled:
                        default:
                        
                            NSLog(@"Cancelled.....");

                        
                            break;
                        case SLComposeViewControllerResultDone:
                        
                            ALERT_VIEW(@"Successfully posted to twitter.");
                        
                            break;
                      ;

                [twitterController setInitialText:text];
                [twitterController setCompletionHandler:completionHandler];
                [self presentViewController:twitterController animated:YES completion:nil];
            

            else
            
                DETweetComposeViewControllerCompletionHandler completionHandler = ^(DETweetComposeViewControllerResult result) 
                    switch (result) 
                        case DETweetComposeViewControllerResultCancelled:
                            NSLog(@"Twitter Result: Cancelled");
                            break;
                        case DETweetComposeViewControllerResultDone:

                            ALERT_VIEW(@"Successfully posted to twitter.");
                            break;
                    
                    [self dismissViewControllerAnimated:YES completion:nil];
                 ;

                DETweetComposeViewController *tcvc = [[[DETweetComposeViewController alloc] init] autorelease];
                self.modalPresentationStyle = UIModalPresentationCurrentContext;
                [tcvc setInitialText:quotesss];
                tcvc.completionHandler = completionHandler;

                [self presentViewController:tcvc animated:YES completion:nil];

            

            break;
        

【问题讨论】:

检查您是否将 Facebook 令牌保存在 NSUserDefaults 中。您使用的是哪个框架? 什么是我不知道的令牌。你能指导我吗? 您使用哪个框架来集成 Facebook 展示你如何实现facebook登录和身份验证的一些代码 【参考方案1】:

从您的代码中,我发现问题出在此代码中,因为 iOS 5 中的 Facebook 在 SLCompose 中不可用,只有 Twitter 可用,因此您的 FaceBook 由 DEFaceBook 处理

            DEFacebookComposeViewControllerCompletionHandler completionHandler = ^(DEFacebookComposeViewControllerResult result) 
                switch (result) 
                    case DEFacebookComposeViewControllerResultCancelled:
                        NSLog(@"Facebook Result: Cancelled");
                        break;
                    case DEFacebookComposeViewControllerResultDone:
                        ALERT_VIEW(@"Successfully posted to facebook.");
                        break;
                

                [self dismissViewControllerAnimated:YES completion:nil];
            ;
            DEFacebookComposeViewController *facebookViewComposer = [[DEFacebookComposeViewController alloc] init];
            self.modalPresentationStyle = UIModalPresentationCurrentContext;

            [facebookViewComposer setInitialText:quotesss];

            facebookViewComposer.completionHandler = completionHandler;
            [self presentViewController:facebookViewComposer animated:YES completion:nil];
            [facebookViewComposer release];
        

所以去你的appdelegate并检查这个方法是否有一个有效的令牌(url包含你的应用程序,令牌格式是applicationURLSchema://"token")检查它是否在这个方法中 如有必要,保留断点并遍历FBSession 类然后handleOpenURL 方法并查找发生错误的位置

 - (BOOL)application:(UIApplication *)application  openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
 annotation:(id)annotation 

 // attempt to extract a token from the url
 return [FBSession.activeSession handleOpenURL:url];
 

【讨论】:

以上是关于如何在 Facebook 上分享帖子时解决登录问题的主要内容,如果未能解决你的问题,请参考以下文章

在 facebook android 上分享帖子

Facebook 帖子分享弹出窗口

无需登录即可查看 Facebook 用户的帖子和分享 [关闭]

如果 Facebook Feed 对话框已弃用,您如何在 Facebook 上分享帖子?

无法将我的 WordPress 帖子与我在 Facebook 上的图片分享

在 Facebook 帖子上分享文字