2012-03-13 128 views
0

我是StackOverflow和iOS的新手。我试图获取用户信息,如团体,书籍等,我需要使用Facebook图形API。要使用图形API,您必须使用访问令牌。我在Facebook开发者页面上阅读了关于这个主题的文章,并且我编写了一些代码,但它没有用。你可以帮我吗?我的代码是正确的还是应该用另一种方式?我无法使用Facebook访问令牌在iOS上使用图形API

我的.h文件:

#import <UIKit/UIKit.h> 
#import "FBConnect.h" 

@interface FirstViewController : UIViewController <FBSessionDelegate,FBLoginDialogDelegate,FBRequestDelegate>{ 

Facebook *face; 


} 

@property(nonatomic,retain)Facebook *face; 
-(IBAction)facePushed:(id)sender; 
-(IBAction)logoutPushed:(id)sender; 

我.m文件:

-(IBAction)facePushed:(id)sender{ 

NSLog(@"FacePushed Began"); 
face=[[Facebook alloc]initWithAppId:@"313714785358239" andDelegate:self]; 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) { 
    face.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
    face.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
}  
if (![face isSessionValid]) { 
    [face authorize:nil ]; 
} 
else { 
    NSLog(@"Session is valid"); 
    } 
    NSLog(@"FacePushed End"); 
} 

} 
- (void)fbDidLogin { 
NSLog(@"aslan"); 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setObject:[face accessToken] forKey:@"FBAccessTokenKey"]; 
[defaults setObject:[face expirationDate] forKey:@"FBExpirationDateKey"]; 
NSLog(@"%@", [face accessToken]); 
[defaults synchronize]; 

} 

- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate { 
    NSLog(@"%@",token); 


} 

回答

1

你必须检查你的执行以下操作:

  1. 你需要获得一个在授权您的应用程序之前的权限

    if (![face isSessionValid]) 
    {   
        NSArray *permissions = [[NSArray alloc] initWithObjects: 
             @"user_likes", 
             @"read_stream", 
             @"publish_stream", 
             nil]; 
        [face authorize:permissions]; 
    
        [permissions release]; 
    } 
    
  2. 您需要在应用程序委托中添加以下函数(facebook对象是您的FaceBook类的实例值)。

    // Pre 4.2 support 
    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
    { 
        return [facebook handleOpenURL:url]; 
    } 
    
    // For 4.2+ support 
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
    { 
        return [facebook handleOpenURL:url]; 
    } 
    
  3. 在您的info.plist

    添加到URL类型> URL方案>与FB前缀您Facebok的应用ID(最终你的价值会是这样fb313714785358239)。

祝你好运。

+0

它没有解决我的问题..但我改善了我的code.thanks。 mycode不使用delegetes我不能看到我的nslogs。 – yatanadam 2012-03-13 15:44:04

+0

单击facePush按钮时发生了什么? – 2012-03-13 15:45:29

+0

我把2个更多的nslog我的代码第一个是开始facepushed第二个结束了facepushed。当我推开脸,我看到了我的新nslogs。别的。并打开脸登录页面,并说这个应用程序想使用喜欢等允许或不我点击允许。只是。 – yatanadam 2012-03-13 15:57:56

相关问题