[英]How to post to Facebook without showing dialog?
我正在做一個閱讀器應用程序,可以將文本發送到Facebook。 但是我在另一頁中完成了登錄Facebook的操作。 我有一個名為“ textpostingbtn
”的按鈕和一個保存文本的textview。 我需要做的是,當用戶點擊按鈕時,它將文本發布到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;
}*/
}
}
好了,您只需登錄一次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
關於SO的討論
在iOS上的NSUserDefaults中保存Facebook access_token
要發布將發送給您的代碼,請按以下按鈕:
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
遵循本教程: https : //developers.facebook.com/docs/mobile/ios/build/如果您按照他們的說法進行操作,那么FB連接對於您的應用程序將是全局的。 使用appDelegate,您可以從應用程序中的任何視圖控制器發布,閱讀和執行您想做的任何事情
您可以為此使用圖形API,這是教程:-https://github.com/reallylongaddress/iPhone-Facebook-Graph-API
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.