使用图形API从Facebook获取用户信息

时间:2012-05-16 10:12:51

标签: iphone facebook facebook-graph-api fbconnect facebook-ios-sdk

我想从Facebook获取基本用户信息,但在以下代码中遇到一些问题

-(void)checkForAccessToken:(NSString *)urlString {
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"access_token=(.*)&" options:0 error:&error];
    if (regex != nil) {
        **NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
        if (firstMatch) {
            NSRange accessTokenRange = [firstMatch rangeAtIndex:1];
            NSString *accessToken = [urlString substringWithRange:accessTokenRange];
            accessToken = [accessToken stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [_delegate accessTokenFound:accessToken];               
        }**
    }
}

在firstMatch中我得到NULL,这就是为什么[_delegate accessTokenFound:accessToken];这种方法不会被调用。

任何人都可以帮助我,以便我能够从Facebook上获取信息

提前致谢。

1 个答案:

答案 0 :(得分:3)

使用app id和credentials.-

初始化后调用fbdid登录方法
(void)fbDidLogin {

NSLog(@"fbDidLogin");
isFacebookLoaded=YES;


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSLog(@"i am in and my acees token is %@ %@ and call me.CALLshowAloadingView",[facebook accessToken],[facebook expirationDate]) ;
[self.facebook requestWithGraphPath:@"me" andDelegate:self];
if ( [delegate respondsToSelector:@selector(showAloadingView)] ) 
{
    NSLog(@" CALLshowAloadingView");
    // calling delegate method. For this method to function, the delegate should be implemented in the calling class.
    [delegate showAloadingView];                
}


// Inform the delegate that Login is successful
if ( [delegate respondsToSelector:@selector(loginStatus:)] ) {
    // calling delegate method. For this method to function, the delegate should be implemented in the calling class.
    [delegate loginStatus:YES];                 
    return;
}   

}