代码之家  ›  专栏  ›  技术社区  ›  Jaka Jančar

如何在iPhone网络应用程序中使用Facebook Connect

  •  22
  • Jaka Jančar  · 技术社区  · 16 年前

    网络应用 ,因此它看起来类似于使用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。

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

    webView:shouldStartLoadWithRequest: 委托方法和检查 fbconnect://success fbconnect://cancel .

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

    我可以从父页面打开到服务器的Comet连接,并让Facebook重定向到我的服务器,然后在成功时通知父页面。但是我不喜欢仅仅因为这个就把服务器组件侵入我的webapp。

    有人有什么聪明的想法吗?

    编辑:这是关于 网络应用 ,而不是本机应用程序。

    7 回复  |  直到 14 年前
        1
  •  1
  •   steve    14 年前

    好的,只是一个小测试,但我试过:

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

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

    PusherApp (我已经在我的应用程序中使用了它),所以如果我不能在iframe SRC属性更改后找出如何触发事件,我可能只会将“auth_successed”事件推送到客户端,并以这种方式处理它。

    谢谢你的建议。我想知道如何在iphonewebapp中支持FB-Connect,而不产生Safari窗口并将其从我的应用程序中删除。

        2
  •  1
  •   Abhinav    14 年前
    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.
    
        4
  •  0
  •   Anna Billstrom    14 年前

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

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

    在参考iFrame中——我也为FB应用程序做过,但在画布上,而不是在Web mobiel中——你链接到登录url作为“_top”,然后传入,你重定向回iFrame(给它一个名称),其中包含各种签名的_请求数据。是的,这里有服务器脚本来处理登录/注销状态(如果这是您上面提到的)。

        6
  •  -1
  •   Tarun    14 年前

    使用Sharekit是一个很好的选择,直到你想摆脱这个问题。因为它为您提供现成的课程。简单地调用方法就足够了,即使对于其他社交网站,比如Twitter。此外,您可以根据需要向其中添加自定义方法。

        7
  •  -1
  •   Ashvin    14 年前