2012-04-29 20 views
4

我是iOS開發新手。我遇到了一個問題,那就是在我的應用中集成facebook API時,fbDidLogin沒有被調用。當我從主視圖控制器(也就是應用程序委託調用的那個)調用API時,一切正常,但在我的情況下,它在設置視圖控制器下。我從字面上複製粘貼代碼從主視圖控制器(它的工作)到設置視圖控制器,它不工作。當不在主視圖控制器上時調用fbDidLogin

我做了這個方法添加到應用程序委託我的理解是對大多數人的問題,但對我來說,我認爲這個問題是在其他地方...任何幫助將是真棒。

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
return [[settingsvc facebook] handleOpenURL:url]; 

} 

凡在應用程序委託頭:

@property (strong, nonatomic) Settings *settingsvc; 
+0

Facebook會話在哪裏實施?在應用程序代表? –

回答

6

有幾件事。

- 您只需要在整個應用程序一個點來實現fbDidLogin。您不會在使用Facebook的每個視圖控制器上實現它。

- 假設您是從Facebook的iOS文檔的入門步驟,你的Facebook對象的生活爲您的應用程序委託的屬性。而您的應用程序委託中您的fbDidLogin方法僅爲。這也應該是您的代碼中聲明Facebook變量的唯一位置。這意味着,只要你從代碼中的其他任何地方訪問Facebook,你做這樣的事情:

ProjectNameAppDelegate *appDelegate = (ProjectNameAppDelegate *)[UIApplication sharedApplication].delegate; 
[appDelegate.facebook someMethod]; 

在您的應用程序委託 - 您handleOpenURL和的OpenURL方法應該看起來就像從Facebook的樣例文檔中,而不是像您發佈的代碼。

-Personally我更喜歡移動的Facebook的對象從應用程序委託的成單輔助類,但如果你是一個初學者,那麼這是可能比你需要更先進。

最新信息答疑評論:

-someMethod無論是Facebook的方法,你正在尋找調用(後牆上,認證,等等)

- 你應該把所有FBSessionDelegate方法在應用程序委託

- 您需要實現您的視圖控制器上FBDialogDelegate您發佈至Facebook

- 然後在您的視圖控制器,當你想要發佈到用戶的牆上做一些升IKE在此:

- (void)postToWall { 
    ProjectNameAppDelegate *appDelegate = (ProjectNameAppDelegate *)[UIApplication sharedApplication].delegate; 
    if (appDelegate.facebook.isSessionValid) { 
     //we're logged in so call the post dialog 
     [self doPost:nil]; 
    } else { 
     //we're not logged in so call the login and then do the post when it's done 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doPost:) name:@"FBLoginComplete" object:nil]; 
     NSArray *permissions = [[NSArray alloc] initWithObjects: 
           @"offline_access", 
           @"publish_stream", 
           nil]; 
     [appDelegate.facebook authorize:permissions]; 
     [permissions release]; 

    } 
} 

-(void)doPost:(NSNotification *) notification { 
    ProjectNameAppDelegate *appDelegate = (ProjectNameAppDelegate *)[UIApplication sharedApplication].delegate; 
    if (appDelegate.facebook.isSessionValid) { 
     NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
             something, @"app_id", 
             something, @"link", 
             something, @"picture", 
             something, @"name", 
             something, @"caption", 
             something, @"description", 
             nil]; 
     [appDelegate.facebook dialog:@"feed" andParams:params andDelegate:self]; 
     [params release]; 
    } 
    [[NSNotificationCenter defaultCenter] removeObserver:self];  
} 

- 對於上述工作,你需要發佈NSNotificationCenter消息「FBLoginComplete」無論何時登錄完成(成功或失敗)。

- 注意:我寫了上面的代碼無需編譯它或測試,以便它可能不是完美的(但通常是:)

- 請注意:您不能發佈到用戶的牆的情況下直接將dailog被彈出。用戶總是能夠在發佈帖子之前編輯帖子。

+0

這是一個很好的答案......什麼應該是「someMethod」btw? – moshikafya

+0

@moshikafya在我看來,「someMethod」是指你需要從Facebook的行動或信息,如「 - (BOOL)isLogin; - (void)login; - (void)註銷; - (void)connectFacebook ;(void)sharedFacebook:(YourStructure)參數「 – Sakares

+0

我只想調用fbdidlogin,以便我可以在用戶的​​牆上發佈消息。我是否應該將所有委託方法(fbdidlogin等)也放在應用程序委託中? – moshikafya

1

你已經實現了代表團FBRequestDelegateyourViewController.h未MainViewController?

FBRequestDelegate含有許多有益的Facebook請求處理機如。

//Called just before the request is sent to the server. 
- (void)requestLoading:(FBRequest *)request; 

//Called when the server responds and begins to send back data 
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response; 

//Called when an error prevents the request from completing successfully. 
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error; 

/** 
    *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 thee format of the API response. 
    */ 
- (void)request:(FBRequest *)request didLoad:(id)result; 

/** 
    *Called when a request returns a response. 
    *The result object is the raw response from the server of type NSData 
    */ 
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data; 

希望它可以幫助你!

+0

@TommyG我猜,你錯過了設置視圖的appdelegate屬性。試着把這個代碼放到屬性爲「AppDelegate * mainDelegate;」的yourSettingViewController.h中並使用「mainDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]」設置yourSettingViewController.h ViewdidLoad方法;「 。如果它不能幫助你更好。我爲我令人困惑的答案感到非常抱歉。 – Sakares

相關問題