简体   繁体   English

ios Facebook用户信息

[英]ios facebook user info

Can you help me how can I get user info. 您能帮我怎样获取用户信息。

  NSString *name;
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
    {
        if (!error)
        {
            // Success! Include your code to handle the results here
           name = [result objectForKey:@"first_name"]; // Error!!! how to get data from this handler
        }
        else
        {
            // An error occurred, we need to handle the error
            // See: https://developers.facebook.com/docs/ios/errors
        }

    }];

Code described above - asynchronous? 上述代码-异步? How to make it synchronous? 如何使其同步? Tell me about the mechanism or tell me where to read. 告诉我有关机制或告诉我在哪里阅读。 Thank you! 谢谢!

You could read everything about Facebook SDK on this site: https://developers.facebook.com . 您可以在以下站点上阅读有关Facebook SDK的所有信息: https : //developers.facebook.com

They don't provide synchronous API and I don't even know why you might need it. 他们不提供同步API,甚至不知道您为什么需要它。 But if you really do you can do some workaround. 但是,如果确实如此,您可以采取一些解决方法。 See implementation: 查看实现:

__block id result = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id theResult, NSError *error) {
    result = theResult;
    dispatch_semaphore_signal(semaphore);    // UPDATE: dispatch_semaphore_wait call was here, which is wrong
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"%@", result);// this code will be launched after you've received response from Facebook

Result conforms FBGraphUser protocol. 结果符合FBGraphUser协议。 So if you it doesn't contain value for first_name key, user wouldn't have specified it. 因此,如果您不包含first_name键的值,则用户不会指定它。 You could print result in debugger and see what it is. 您可以在调试器中打印结果 ,看看它是什么。

You can use additional method to solve your problem: 1) make name as property: __block NSString *name 2) move you code, that you need to perform, to separate method, like that: 您可以使用其他方法来解决问题:1)将name设置为属性:__block NSString * name 2)将需要执行的代码移到单独的方法中,如下所示:

- (void) methodWithComplitionHandler
{
name = nil;
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
        if (!error)
        {
           name = [result objectForKey:@"first_name"]; 
        }
        else
        {
        }
}];
[self futherProcessMethod];
}

- (void) furtherProcessMethod
{
if (self.name == nil)
    {
        [self performSelector:@selector(furtherProcessMethod) withObject:nil afterDelay:3.0]; // here set appropriate delay
    }
    else
    {
      // do some with name;
    }
}

As I clarify in previous answer, you need to work with name inside "else" statement. 正如我在上一个答案中阐明的那样,您需要在“ else”语句中使用名称。 Please, debug for more understanding. 请调试以获取更多理解。 So we have: 因此,我们有:

- (void) SetUserData
{
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
    {
        if (!error)
        {
            self.first_name  = [result objectForKey:@"first_name"];
        }
        else
        {
        }
    }];
    [self furtherProcessMethod];

}


- (void) furtherProcessMethod
{
    if (self.first_name == nil)
    {
        [self performSelector:@selector(furtherProcessMethod) withObject:nil afterDelay:30.0]; // here set appropriate delay
    }
    else
    {
        NSLog(@"first_name: %@", self.first_name); 
    }
}

Seems like you totally mess up. 好像您完全搞砸了。 Try this code, but be sure, your understand it: 尝试下面的代码,但是请确保您理解它:

- (void) SetUserData
{
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
    {
        if (!error)
        {
            self.first_name  = [result objectForKey:@"first_name"];
            NSLog(@"First name just came from server: first_name: %@", self.first_name); 
        }
        else
        {

        }
    }];
    [self furtherProcessMethod];
}


- (void) furtherProcessMethod
{
    if (self.first_name == nil)
    {
        // this is not recursion, do not miss that!!
        [self performSelector:@selector(furtherProcessMethod) withObject:nil afterDelay:2.0]; 
    }
    else
    {
        NSLog(@"First name that I can work with first_name: %@", self.first_name); 
// here you can handle you first_name, after it came
    }
}

So after some time you need to get in log: First name just came from server: first_name: some name First name that I can work with first_name: some name 因此,一段时间后,您需要登录:名字刚好来自服务器:first_name:一些名字我可以使用的名字first_name:一些名字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM