2012-03-13 57 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

相關問題