我们在程序中经常会用facebook分享应用和一些好玩的东西,fb给我们提供了相应的api,但是在中国由于大防火墙的原因,我们不能正常访问fb网站,需要使用代理和vpn。
本文参考:http://www.mobisoftinfotech.com/blog/iphone/iphone-fbconnect-facebook-connect-tutorial/
实现的效果如图:
实现过程如下:
- 下载fb关于ios的zip包,点击下载地址,然后解压缩.
- 然后双击解压缩文件下的src/FBConnect.xcodeproj,在xcode中打开即可.
- 新建一个fb的demo工程,我的叫iphone.facebook,然后正确编译,运行即可.
- 然后从 FBConnect工程中拷贝FBConnect文件夹到你的新工程中即可.
- 最关键的一步是配置环境,找到你下载的文件包的src目录,然后添加到User Header Search Path中.如下图:
其中我下载的路径是:/Users/vsp/Downloads/facebook-facebook-iphone-sdk-1059eb6/src
- 编译程序,能够正常编译,没有错误即可.
- 在fb网站上申请自己的开发应用:(http://www.facebook.com/developers/)
- 下面就是在新工程中添加一个按钮,并定义相应的点击事件即可.
在controller中添加:
#import <UIKit/UIKit.h>
#import "FBConnect/FBConnect.h"
#import "FBConnect/FBSession.h"
#define FB_APP_KEY @"YOUR_APP_KEY" //自己申请的
#define FB_SEC_KEY @"YOU_SEC_KEY" //自己申请的@interface iphone_facebookViewController : UIViewController {
IBOutlet FBLoginButton *fbButton;
FBSession *fbSession;
id<FBRequestDelegate> delegate;
int status;
}-(IBAction)clickButton:(id)sender;
@property (nonatomic, retain) FBSession *fbSession;
@property(nonatomic,assign) id<FBRequestDelegate> delegate;@end
相应的方法:
#import "iphone_facebookViewController.h"
@implementation iphone_facebookViewController
@synthesize fbSession;
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
status = 0 ;
if ( self.fbSession == nil ) {
self.fbSession = [FBSession sessionForApplication:FB_APP_KEY secret:FB_SEC_KEY delegate:self ];
}
}- (void)session:(FBSession*)session didLogin:(FBUID)uid {
status = 1;
NSString* fql = [NSString stringWithFormat:@"select uid,name from user where uid == %lld",self.fbSession.uid];
NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
[[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
}
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([request.method isEqualToString:@"facebook.fql.query"]) {
NSString * username = [ ((NSDictionary*)[(NSArray*)result objectAtIndex:0 ]) objectForKey:@"name"] ;
// showLabel.text = [NSString stringWithFormat:@"Hi, %@" , username ];
}
}
-(IBAction)clickButton:(id)sender
{
NSLog(@"点击了按钮");
if ( status == 0 ) { // do login
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:self.fbSession] autorelease];
[dialog show];
}
else { // do post to Wall
FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.delegate = self;
dialog.userMessagePrompt = @"Example prompt";
dialog.attachment = @
"{"
"/"name/":/"Facebook Connect for iPhone/","
"/"href/":/"http://developers.facebook.com/connect.php?tab=iphone/","
"/"caption/":/"Caption/","
"/"description/":/"Description/","
"/"media/":[{"
"/"type/":/"image/","
"/"src/":/"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg/","
"/"href/":/"http://www.facebook.com/" rel="nofollow""
"}"
"}"
"}";
// dialog.targetId = @"999999"; // replace this with a friend’s UID
[dialog show];
}
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}- (void)viewDidUnload {
}
- (void)dealloc {
//[self.fbButton release];
// [self.fbSession release];
[super dealloc];
}@end