【问题标题】:How to use Facebook Connect in an iPhone webapp如何在 iPhone 网络应用程序中使用 Facebook Connect
【发布时间】:2010-12-31 09:38:10
【问题描述】:

我正在尝试通过 iPhone webapp 使用 Facebook Connect,因此它看起来类似于在使用 Facebook Connect iPhone SDK 的原生 iPhone 应用程序中的外观。

iPhone SDK 通过访问获得漂亮的登录页面:

http://www.facebook.com/login.php?fbconnect=1&connect_display=touch&api_key=<key>&next=fbconnect://success

(见http://github.com/facebook/facebook-iphone-sdk/blob/master/src/FBLoginDialog.m

由于我不想打开 Safari,我看到显示此页面的唯一方法是使用 iframe。

然后就是检测成功和失败的问题。

SDK 可以通过简单地使用webView:shouldStartLoadWithRequest: 委托方法并检查fbconnect://successfbconnect://cancel 来做到这一点。

据我所知,父页面无法监控iframe的URL。

我可以从父页面打开一个到服务器的 Comet 连接,并让 Facebook 重定向到我的服务器,然后它会通知父页面成功。但我不喜欢仅仅因为这个而将服务器组件引入我的 web 应用程序的想法。

有人有什么聪明的主意吗?

编辑:这是关于网络应用,而不是原生应用。

【问题讨论】:

  • 您找到解决此问题的方法了吗? OpenID 也有同样的问题,很想看看你想出了什么。谢谢!
  • 我实际上正要发布这个完全相同的问题。我没想过把它放在 iframe 中——这是个好主意。

标签: iphone facebook web-applications


【解决方案1】:

听起来您或许可以通过 OAuth 2.0 来实现这一点。 Facebook 文档很薄——在http://www.raywenderlich.com/1488/how-to-use-facebooks-new-graph-api-from-your-iphone-app 上有一个很好的讨论

那篇博文是关于在 iPhone 应用程序中使用 OAuth 和新的 Graph API,但我发现 OAuth 讨论比其他任何地方都好。您也许可以使用他在您的 web 应用程序中讨论的 URL 和流程来完成同样的事情。

【讨论】:

    【解决方案2】:

    好吧,这只是一个小测试,但我试过了:

    var iframe = $("<iframe src='http://google.com'></iframe");
    $("body").append(iframe);
    $("body").find("iframe:last").attr("src"); // => http://google.com
    

    而且它似乎工作正常。我遇到了同样的问题,所以今晚我将在我的应用程序中尝试同样的解决方案。我不想做一个不断检查 SRC 的间隔,所以我会看看我是否可以在该 SRC 更改时触发事件。

    处理 Comet 的另一种方法是 WebSockets。一个不错的、易于处理的服务是PusherApp(无论如何我已经在我的应用程序中使用它),所以如果我不知道如何在 iframe SRC 属性更改后触发事件,我可能只是推送向客户端发送“auth_succeeded”事件并以这种方式处理。

    感谢 iframe 的想法。我想知道如何在 iPhone Web 应用程序中支持 FB Connect,而无需生成 Safari 窗口并将它们从我的应用程序中删除。

    【讨论】:

    • iframe的src不会改变。
    【解决方案3】:

    在您只想摆脱问题之前,使用 Sharekit 是一个不错的选择。因为它为您提供现成的课程。即使对于其他社交网站以及 Twitter 来说,简单地调用方法就足够了。此外,您可以根据需要添加自定义方法。

    【讨论】:

      【解决方案4】:

      您可以单独使用每个社交网络 API,也可以使用 ShareKit

      【讨论】:

        【解决方案5】:

        我已经在 iOS 和 Android 上完成了一个移动网络应用程序,具有所有 Facebook 连接。当然,它全部托管在服务器上,而不是作为 Web 应用程序部署在 iOS 设备上。

        基本思路是:使用 FB Javascript SDK 加载登录/注销对话框,将服务器上的结果处理为“已登录”(显示某些内容)与“未登录”显示错误消息或未登录的内容。

        在参考 iFrames 中——我也为 FB 应用程序做过,但在画布上不是在 Web mobiel 中——你以“_top”的形式链接到登录 url,然后传入,你重定向回 iFrame(给它一个name) 与各种 signed_request 数据。所以是的,那里有服务器脚本来处理登录/注销状态(如果这就是你上面提到的)。

        【讨论】:

          【解决方案6】:
          【解决方案7】:
          To use facebook from your app just download FBConnect and drag and drop all files of FBConnect to your project.Make sure copy the files in project option is checked.After adding files make your .h file in which you have to share something like this
          
          #import <Foundation/Foundation.h>
          #import "cocos2d.h"
          #import "Facebook.h"
          #import "FBConnect.h"
          
          
          @interface GameOverPage :CCLayer<FBSessionDelegate,FBDialogDelegate,FBLoginDialogDelegate,FBRequestDelegate>
          {
          
           //for sharing
              BOOL isFBLogged;
              Facebook *facebook;
          }
          
          
          after this add following delegate  methods to .m file.To use facebook just call [self facebookLogin];
          
          
          
          - (void) facebookLogin
          {   
          //  App ID
          //    ****************
          //    
          //    App secret
          //    *****************************   
          
              if (facebook == nil) {
                  facebook = [[Facebook alloc] initWithAppId:@"**********************"];
                  NSLog(@"reached in if facebook nill");
              }   
          
          
          NSArray* permissions =  [[NSArray arrayWithObjects:
                                        @"publish_stream", @"offline_access", nil] retain];
          
              [facebook authorize:permissions delegate:self];
          
              // If you want to add Logout capability:
              if (isFBLogged) {
                  NSLog(@"in isFBLogggged");
                  [facebook logout:self];
              }
              //  } else { // then the code above inside the else
          }
          -(void) postdata
          {
              UIImage *facebookimage=[self takeScreenShot];
              NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                             facebookimage,@"message",                                   
                                             nil];
          
              [facebook requestWithGraphPath:@"me/photos"  //  use page ID instead of 'me'
                                   andParams:params
                               andHttpMethod:@"POST"
                                 andDelegate:self];
              NSLog(@"Uploading screenshot. Please wait...");
          }
          
          - (void)fbDidLogin {
              NSLog(@"in fbDidLogin");
              isFBLogged = YES;   
              [self postdata];
          
          }
          -(void)fbDidNotLogin:(BOOL)cancelled {
              if (cancelled) {
          
                  NSLog(@"LOGIN CANCELLED");
              } else {
                  NSLog(@"login error");
          
              }
          
              //self.visible = YES;
          }
          - (void)fbDidLogout {
              NSLog(@"in fbDidLogOut");
              isFBLogged = NO;
              //-facebookLoginButton.visible = YES;
              //-facebookLogoutButton.visible = NO;
          }
          
          #pragma mark -
          #pragma mark FBRequestDelegate
          /**
           * Called when the Facebook API request has returned a response. This callback
           * gives you access to the raw response. It's called before
           * (void)request:(FBRequest *)request didLoad:(id)result,
           * which is passed the parsed response object.
           */
          - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
          
          }
          
          /**
           * Called when a request returns and its response has been parsed into
           * an object. The resulting object may be a dictionary, an array, a string,
           * or a number, depending on the format of the API response. If you need access
           * to the raw response, use:
           *
           * (void)request:(FBRequest *)request
           *      didReceiveResponse:(NSURLResponse *)response
           */
          - (void)request:(FBRequest *)request didLoad:(id)result {   
          
          
              NSLog(@"posted");
          };
          
          /**
           * Called when an error prevents the Facebook API request from completing
           * successfully.
           */
          - (void)request:(FBRequest *)request didFailWithError:(NSError *)error {    
          
              NSLog(@"an error occured");
          
          };
          
          #pragma mark -
          #pragma mark FBDialogDelegate
          
          /**
           * Called when a UIServer Dialog successfully return.
           */
          - (void)dialogDidComplete:(FBDialog *)dialog {
              NSLog(@"publish successfully");
          }
          
          you can override all delegate methods according to your requirement or leave the same way they are.This will work fine.You should have appid of facebook which you will get after registration on Facebook.
          

          【讨论】:

            猜你喜欢
            • 2012-01-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多