Facebook SDK iOS访问代码问题

时间:2013-09-20 11:21:48

标签: ios facebook sdk

我的项目是在我的iPhone应用程序上获取我的Fanpage的Facebook Insights数据。

我开始安装适用于iOS的Facebook SDK以获取访问代码&而不是使用随Facebook iOS SDK提供的SampleLoginSample示例程序。

它的完美与完美给出与Facebook API Explporer for Query

相同的响应
[self.textNoteOrLink setText:
    [NSString stringWithFormat:@"https://graph.facebook.com/[PAGEID]?fields=likes&access_token=%@",appDelegate.session.accessTokenData.accessToken]];

但是,如果我在下面写,请查询它没有像Facebook Explorer API那样给我回复

QUERY:

[self.textNoteOrLink setText:
    [NSString stringWithFormat:@"https://graph.facebook.com/PAGEID/insights/page_fans?access_token=%@",appDelegate.session.accessTokenData.accessToken]];

响应:

{"data":[

],"paging":{

"previous":"https:\/\/graph.facebook.com\/PAGEID\/insights\/page_fans?access_token=CAA...snip...h2l&since=1378984583&until=1379243783","next":"https:\/\/graph.facebook.com\/PAGEID\/insights\/page_fans?access_token=CAA...snip...h2l&since=1379502983&until=1379762183"}}

任何人都可以帮我在Facebook iOS SDK中获取具有get_insights权限的访问代码吗?

1 个答案:

答案 0 :(得分:1)

SLAppDelegate.h

@property列表下,添加:

-(BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

SLAppDelegate.m

applicationDidBecomeActive下,添加:

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"read_insights",
                            nil];

    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}

/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

SLViewController.h

viewDidLoad结束时,添加:

[appDelegate openSessionWithAllowLoginUI:NO];