2011-11-22 56 views
2

我正在做一個可以發送文本到Facebook的閱讀器應用程序。但我完成了在另一個頁面上登錄到Facebook。我有一個名爲'textpostingbtn'的按鈕和一個包含文本的textview。我需要做的是,當用戶點擊按鈕時,它將文本發佈到Facebook而不顯示登錄對話框。我對按鈕進行了一些驗證,因此僅當用戶登錄時才啓用按鈕。link that i used in login purpose如何在不顯示對話框的情況下發布到Facebook?

編輯:

- (IBAction)postGradesTapped:(id)sender { 
    //_posting = YES; 
    // If we're not logged in, log in first... 
    if (![_session isConnected]) { 
     self.loginDialog = nil; 
     _loginDialog = [[FBLoginDialog alloc] init];  
     [_loginDialog show]; 

     _logoutButton.hidden = YES; 
    } 
    // If we have a session and a name, post to the wall! 
    //else if (_facebookName != nil) { 
    //[self postToWall]; 
    //} 
    // Otherwise, we don't have a name yet, just wait for that to come through. 

} 

- (IBAction)logoutButtonTapped:(id)sender { 
    [_session logout]; 
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"FB_logged"]; 
    _btnFacebookmain.enabled =NO; 
} 


#pragma mark FBSessionDelegate methods 

- (void)session:(FBSession*)session didLogin:(FBUID)uid { 
    [self getFacebookName]; 

} 

- (void)session:(FBSession*)session willLogout:(FBUID)uid { 
    _logoutButton.hidden = YES; 
    _facebookName = nil; 
} 

#pragma mark Get Facebook Name Helper 

- (void)getFacebookName { 
    NSString* fql = [NSString stringWithFormat: 
        @"select uid,name from user where uid == %lld", _session.uid]; 
    NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"]; 
    [[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params]; 
} 

#pragma mark FBRequestDelegate methods 

- (void)request:(FBRequest*)request didLoad:(id)result { 
    if ([request.method isEqualToString:@"facebook.fql.query"]) { 
     NSArray* users = result; 
     NSDictionary* user = [users objectAtIndex:0]; 
     NSString* name = [user objectForKey:@"name"]; 
     self.facebookName = name;  
     _logoutButton.hidden = NO; 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FB_logged"]; 
     [_logoutButton setTitle:[NSString stringWithFormat:@"Facebook: Logout as %@", name] forState:UIControlStateNormal]; 
     /*if (_posting) { 
     [self postToWall]; 
     _posting = NO; 
     }*/ 
    } 
} 

回答

1

那麼你必須登錄到FB只有一次。 將訪問令牌保存在NSUserDefaults中。 然後,當你想分享FB時,只需訪問你的NSUserDefaults並獲取訪問令牌並使用該令牌進行共享。

存儲NSUserDefaults的中訪問令牌

 facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self]; 
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) { 
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 

}

下面是將引導您

https://developers.facebook.com/docs/mobile/ios/build/

http://coffeeshopped.com/2011/01/saving-sessions-with-the-facebook-ios-sdk

discussi的幾個環節對SO

Saving Facebook access_token in NSUserDefaults on iOS

How to get a Facebook access token on iOS

要發佈的代碼,會去你按下按鈕將是這樣的: -

 FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease]; 
     dialog.userMessagePrompt = @"Enter your message:"; 
     dialog.attachment = [NSString stringWithFormat:@"I Love iOS"]; 
     [dialog show]; 

這裏是引用鏈接。

http://www.raywenderlich.com/77/how-to-post-on-facebook-with-your-iphone-app

+0

請看看我的編輯問題,這是我如何登錄facebook.code, – ICoder

+0

,如何從此代碼獲取令牌以及如何在我的發佈按鈕中設置另一個視圖控制器?謝謝 – ICoder

+0

postgradetapped是Facebook的登錄按鈕 – ICoder

0

您可以使用圖形API爲此這裏是教程:-https://github.com/reallylongaddress/iPhone-Facebook-Graph-API

相關問題