IOS:Easy Facebook SDK电子邮件检索目标C.

时间:2015-06-18 19:19:56

标签: ios facebook facebook-graph-api

修改 (重新提出问题因为之前没有意义。)

我有一个loginViewController,他跟随IBAction:

-(IBAction)fblogin:(id)sender
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            // Process error
        } else if (result.isCancelled) {
            // Handle cancellations
        } else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if ([result.grantedPermissions containsObject:@"email"]) {
                if ([FBSDKAccessToken currentAccessToken]) {
                    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
                     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                  id result, NSError *error) {
                         if (!error) {
                             NSLog(@"fetched user:%@", result);

                         }
                     }];
                }
                isFB = TRUE;
                [self closePopup];
            }
        }
}];

所以...现在我已经获得了所有用户数据......我该如何使用它?

2 个答案:

答案 0 :(得分:0)

您正在启动异步任务,由于延迟(网络,服务器时间响应等等),您将在执行应用程序指令后获得“结果”。

如果您的意思是问题,可以在应用中添加NSNotification。当FB请求结束时,您可以发送通知并返回所需的电子邮件。 当然,你必须处理这个迟到的答案。 例如,在使用电子邮件值设置UITextField后重新加载视图。 这就是你需要的吗?

答案 1 :(得分:0)

确定。大概你想要在某个地方存储这个用户信息,或者你想立即使用它。无论哪种方式都会在这一行之后发生:

NSLog(@"fetched user:%@", result);

因此,例如在我的情况下,如果我想将所有这些信息发布到数据库:

-(IBAction)fblogin:(id)sender
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            // Process error
        } else if (result.isCancelled) {
            // Handle cancellations
        } else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if ([result.grantedPermissions containsObject:@"email"]) {
                if ([FBSDKAccessToken currentAccessToken]) {
                    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
                     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                  id result, NSError *error) {
                         if (!error) {
                             NSLog(@"fetched user:%@", result);
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                 NSString *post = [NSString stringWithFormat:@"email=%@&firstName=%@&lastName=%@&FBID=%@", [result objectForKey:@"email"], [result objectForKey:@"first_name"], [result objectForKey:@"last_name"], [result objectForKey:@"id"]];

                 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
                 NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];

                 NSString *url = urlLogin;
                 NSLog(@"%@", url);
                 NSURL *url = [NSURL URLWithString:url];
                 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
                 [request setHTTPMethod:@"POST"];
                 NSLog(@"%@", post);
                 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
                 [request setHTTPBody:postData];

                 //[NSURLConnection connectionWithRequest:request delegate:self];
                 NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
                 [conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
                 [conn start];
    });
         }
     }];
}

完成。我只是对这个模型感到困惑,忘记了“等一下!我需要的一切实际上都包含在FBSDKGraphRequest中......我可以在那里处理所有的操作。”

你可以在任何地方做到这一点,但在登录时这样做是有意义的。感谢Can ATAC和GruntCakes让我不能在昨晚疯狂。

相关问题