Facebook Facebook iOS SDK-jak odzyskać odpowiedź na Facebooka

Używam Facebook iOS SDK dla iPhone. Inicjalizuję instancję Facebook

facebook = [[Facebook alloc] initWithAppId:kAppId];

A potem się loguję:

[facebook authorize:permissions delegate:self];

Po zalogowaniu się na Facebook robię następujące, aby uzyskać informacje o Profilu Użytkownika:

[facebook requestWithGraphPath:@"me" andDelegate:self];
NSMutableData *response = [fbRequest responseText];
unsigned char *firstBuffer = [response mutableBytes];
NSLog(@"Got Facebook Profile: : \"%s\"\n", (char *)firstBuffer);

Ale dostaję na konsoli:

Got Facebook Profile: "(null)"

Co robię źle, również wierzę, że odpowiedź Facebook jest ciąg json i szukam, aby zdobyć ten ciąg json.

Author: JasonMArcher, 2011-03-02

2 answers

Pomyślałem, że może powinienem zrobić z tego wiki i powiedzieć ludziom, jak to robię. ponieważ wiele osób boryka się z podobnym problemem.

Pierwszą rzeczą, którą zrobiłem, było Na Facebook ' U.m klasa i dodała następującą instrukcję w następującej metodzie
(void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                             safariAuth:(BOOL)trySafariAuth
 trySafariAuth = NO;

Zapobiega to otwarciu strony safari dla logowania facebook, ale wyskakuje Ekran w samej aplikacji. Następnie utworzyłem klasę pomocniczą dla Facebook, Kod pliku nagłówkowego jest tutaj.

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

@interface FaceBookHelper : UIViewController
<FBRequestDelegate,
FBDialogDelegate,
FBSessionDelegate>{

Facebook    *facebook;
NSArray *permissions;
}


@property(readonly) Facebook *facebook;

- (void)login;

-(void)getUserInfo:(id)sender;

- (void)getUserFriendList:(id)sender;

-(void)postToFriendsWall;

The .na plik.

static NSString* kAppId = @"xxx";
#define ACCESS_TOKEN_KEY @"fb_access_token"
    #define EXPIRATION_DATE_KEY @"fb_expiration_date"

@implementation FaceBookHelper

@synthesize facebook;

//////////////////////////////////////////////////////////////////////////////////////////////////
// UIViewController

/**
 * initialization
 */
- (id)init {
    if (self = [super init]) {
        facebook = [[Facebook alloc] initWithAppId:kAppId];
        facebook.sessionDelegate = self;
        permissions =  [[NSArray arrayWithObjects:
                              @"email", @"read_stream", @"user_birthday", 
                              @"user_about_me", @"publish_stream", @"offline_access", nil] retain];
        [self login];
    }
    return self;

}


///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (void)dealloc {
    [facebook release];
    [permissions release];
    [super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// private

/**
 * Login.
 */
- (void)login {
    // only authorize if the access token isn't valid
    // if it *is* valid, no need to authenticate. just move on
    if (![facebook isSessionValid]) {
           [facebook authorize:permissions delegate:self];
    }
}

/**
 * This is the place only where you will get the hold on the accessToken
 *
 **/
- (void)fbDidLogin {
    NSLog(@"Did Log In");
    NSLog(@"Access Token is %@", facebook.accessToken );
    NSLog(@"Expiration Date is %@", facebook.expirationDate );
    // Store the value in the NSUserDefaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:facebook.accessToken forKey:ACCESS_TOKEN_KEY];
    [defaults setObject:facebook.expirationDate forKey:EXPIRATION_DATE_KEY];
    [defaults synchronize];
    // This is the best place to login because here we know that user has already logged in
    [self getUserInfo:self];
    //[self getUserFriendList:self];
    //[self postToFriendsWall];
}

- (void)fbDidNotLogin:(BOOL)cancelled {
    NSLog(@"Failed to log in");
}

    - (void)getUserInfo:(id)sender {
      [facebook requestWithGraphPath:@"me" andDelegate:self];
    }

    - (void)getUserFriendList:(id)sender {
      [facebook requestWithGraphPath:@"me/friends" andDelegate:self];
    }
////////////////////////////////////////////////////////////////////////////////
// FBRequestDelegate

/**
 * Called when the Facebook API request has returned a response. This callback
 * gives you access to the raw response. It's called before
 * (void)request:(FBRequest *)request didLoad:(id)result,
 * which is passed the parsed response object.
 */
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Inside didReceiveResponse: received response");
    //NSLog(@"Status Code @", [response statusCode]);
    NSLog(@"URL @", [response URL]);
}

/**
 * Called when a request returns and its response has been parsed into
 * an object. The resulting object may be a dictionary, an array, a string,
 * or a number, depending on the format of the API response. If you need access
 * to the raw response, use:
 *
 * (void)request:(FBRequest *)request
 *      didReceiveResponse:(NSURLResponse *)response
 */
- (void)request:(FBRequest *)request didLoad:(id)result {
    NSLog(@"Inside didLoad");
    if ([result isKindOfClass:[NSArray class]]) {
        result = [result objectAtIndex:0];
    }
    // When we ask for user infor this will happen.
    if ([result isKindOfClass:[NSDictionary class]]){
        //NSDictionary *hash = result;
        NSLog(@"Birthday: %@", [result objectForKey:@"birthday"]);
        NSLog(@"Name: %@", [result objectForKey:@"name"]); 
    }
    if ([result isKindOfClass:[NSData class]])
    {
        NSLog(@"Profile Picture");
        //[profilePicture release];
        //profilePicture = [[UIImage alloc] initWithData: result];
    }
    NSLog(@"request returns %@",result);
    //if ([result objectForKey:@"owner"]) {}

};

/**
 * Called when an error prevents the Facebook API request from completing
 * successfully.
 */
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
  //[self.label setText:[error localizedDescription]];
};


////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate

/**
 * Called when a UIServer Dialog successfully return.
 */
- (void)dialogDidComplete:(FBDialog *)dialog {
//[self.label setText:@"publish successfully"];
}

@end
 25
Author: Yogesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-13 12:54:24

Dzięki za podpowiedź Yogesh!

In facebook.m Możesz również ustawić safariAuth param w metodzie authorize.

- (void)authorize:(NSArray *)permissions
     delegate:(id<FBSessionDelegate>)delegate {

  ...

  [self authorizeWithFBAppAuth:YES safariAuth:NO];
}
 2
Author: Martin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-13 06:18:05