如何使用 Facebook iOS SDK 检索 Facebook 响应

发布于 2024-10-19 17:05:48 字数 664 浏览 3 评论 0原文

我正在使用适用于 iPhone 的 Facebook iOS SDK。我初始化 Facebook 实例

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

然后登录:

[facebook authorize:permissions delegate:self];

登录 Facebook 后,我正在执行以下操作来获取用户个人资料信息:

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

但是我在控制台上看到以下内容:

Got Facebook Profile: "(null)"

我做错了什么,我也相信 Facebook 的响应是一个 json 字符串,我希望获取该 json 字符串。

I am using the Facebook iOS SDK for iPhone. I initialize the Facebook instance

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

And then I do login:

[facebook authorize:permissions delegate:self];

After I logged in to Facebook I am doing the following to get the user profile information:

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

But I get the following on my console:

Got Facebook Profile: "(null)"

What am I doing wrong, also I believe that Facebook response is a json string and I am looking to get a hold of that json string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-10-26 17:05:48

我想也许我应该把它做成一个维基并告诉人们我是如何做的。因为很多人都面临着类似的问题。

我做的第一件事是。

在 Facebook.m 类中,我在以下方法中添加了以下语句,

(void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                             safariAuth:(BOOL)trySafariAuth
 trySafariAuth = NO;

这可以防止打开 Safari 页面以进行 facebook 登录,但它会在应用程序本身中弹出一个屏幕。然后我为Facebook创建了一个辅助类,头文件代码在这里。

#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;

.m 文件。

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

I thought may be I should make it a wiki and tell the people how I am doing it. because lot of people are facing similar problem.

The first thing that I did was.

In Facebook.m class I added the following statement in the following method

(void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                             safariAuth:(BOOL)trySafariAuth
 trySafariAuth = NO;

This prevents a safari page to get open for the facebook login, but it pops up a screen in app itself. Then i created a helper class for Facebook, the header file code is here.

#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 .m file.

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
月寒剑心 2024-10-26 17:05:48

感谢 Yogesh 的提示!

facebook.m 中,您还可以在授权方法中设置 safariAuth 参数。

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

  ...

  [self authorizeWithFBAppAuth:YES safariAuth:NO];
}

Thanks for this hint Yogesh!

In facebook.m you can also set the safariAuth param in the authorize method.

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

  ...

  [self authorizeWithFBAppAuth:YES safariAuth:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文