2009-12-31 33 views
22

我想使用Facebook的連接從iPhone webapp,所以它看起來很像它看起來在使用Facebook連接iPhone SDK的本機iPhone應用程序。如何使用Facebook的連接在iPhone的網絡應用程序

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瀏覽器打開,我看到顯示此頁面是唯一的出路:

iPhone SDK的訪問得到了很好的登錄頁面通過使用iframe。

然後有檢測成功和失敗的問題。

SDK可以通過簡單地使用webView:shouldStartLoadWithRequest:委託方法並檢查fbconnect://successfbconnect://cancel來完成此操作。

據我所知,父頁面不能監視iframe的URL。

我可以從父頁面打開Comet連接到服務器,並將Facebook重定向到我的服務器,然後該服務器會在成功時通知父頁面。但是,我不喜歡僅僅因爲這個而將服務器組件引入我的web應用程序的想法。

有沒有人有任何聰明的想法?

編輯:這是關於一個網絡應用程序,而不是本機應用程序。

+0

你找到一個解決這個問題?有與OpenID相同的問題,並有興趣看看你想出了什麼。謝謝! – Cimm 2010-09-25 17:47:58

+0

我實際上是要發佈這個完全相同的問題。我沒有想到將它放在iframe中 - 這是一個好主意。 – steve 2011-09-07 23:41:02

回答

1

行,所以只是小試,但我想這一點:

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窗口並將其從我的應用程序中取出。

+0

iframe的src不會更改。 – 2011-09-08 05:59:38

-1

使用Sharekit是一個很好的選擇,直到你只是想擺脫這個問題。因爲它爲你提供了現成的課程。即使對於其他社交網站以及Twitter也只需調用方法即可。此外,您可以根據需要添加自定義方法。

-1

您可以單獨使用每個社交網絡API,或者您可以使用ShareKit

0

我已經做了移動網絡應用程序,iOS和Android上,所有的Facebook的連接。當然,它全部託管在服務器上,並未作爲Web應用程序部署在iOS設備上。

基本思想是:使用FB Javascript SDK加載登錄/註銷對話框,在服務器上處理結果作爲「已登錄」(顯示內容)與「未登錄」顯示錯誤消息或未登錄的內容。

在引用iFrames - 我也爲FB應用程序完成的,但不是在Web mobiel的畫布上 - 您鏈接到登錄網址爲「_top」,並且傳入,您重定向回iFrame(給它一個名字)與各種signed_request數據。所以是的,那裏有服務器腳本來處理登錄/註銷狀態(如果這就是你上面提到的)。

1
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. 
相關問題