iphone调用facebook相应的api

我们在程序中经常会用facebook分享应用和一些好玩的东西,fb给我们提供了相应的api,但是在中国由于大防火墙的原因,我们不能正常访问fb网站,需要使用代理和vpn。

本文参考:http://www.mobisoftinfotech.com/blog/iphone/iphone-fbconnect-facebook-connect-tutorial/

实现的效果如图:

IMG_0026 IMG_0027 IMG_0028

 

实现过程如下:

  • 下载fb关于ios的zip包,点击下载地址,然后解压缩.
  • 然后双击解压缩文件下的src/FBConnect.xcodeproj,在xcode中打开即可.
  • 新建一个fb的demo工程,我的叫iphone.facebook,然后正确编译,运行即可.
  • 然后从 FBConnect工程中拷贝FBConnect文件夹到你的新工程中即可.

image

  • 最关键的一步是配置环境,找到你下载的文件包的src目录,然后添加到User Header Search Path中.如下图:

image其中我下载的路径是:/Users/vsp/Downloads/facebook-facebook-iphone-sdk-1059eb6/src

    image

  • 下面就是在新工程中添加一个按钮,并定义相应的点击事件即可.

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值