【问题标题】:Using Facebook iOS SDK to retrieve birthdays使用 Facebook iOS SDK 检索生日
【发布时间】:2012-04-25 21:16:13
【问题描述】:

我是使用 API 的新手,所以我不确定如何正确执行此操作。

我正在尝试获取用户朋友的所有生日。到目前为止,我已经成功地获得了他们的朋友列表,然后理论上我可以简单地 ping 每个 ID 并从中获取生日。

但是我不确定如何实现这些方法。

当我的 viewController 加载时:[facebook requestWithGraphPath:@"me/friends" andDelegate:self];

这会发送请求,然后我实现 FBRequestDelegate 方法来接收它:

-(void)request:(FBRequest *)request didLoad:(id)result{
    NSLog(@"result is : %@", result);
}

到目前为止效果很好,我得到了一个包含所有朋友姓名和 ID 的对象。但是,现在我想遍历每个 ID,然后再发送几百个请求。我已经知道如何设置循环和请求调用,但是我已经在这个 viewController 中使用了 didLoad 方法,而且我显然需要在数据对象返回后以不同的方式处理数据。

与 (FBRequest *) 有什么关系?那是什么,也许我可以像 if(request == something) 这样的东西?谢谢

【问题讨论】:

    标签: iphone ios xcode facebook-graph-api


    【解决方案1】:

    您可以通过一个 fql 请求检索您(或用户)的 Facebook 朋友的生日。

    您可以在此处阅读有关 fql 的信息:http://developers.facebook.com/docs/reference/fql/

    但这里有一些代码供参考(此代码在定义为 FBSessionDelegate 和 FBRequestDelegate 的类中)

    - (void)request:(FBRequest *)request didLoad:(id)result {
        // result is a NSDictionary of your friends and their birthdays!
    }
    
    - (void)fbDidLogin {
        //some best practice boiler plate code for storing important stuff from fb
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
        [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    
        //now load all my friend's birthdays
        NSMutableDictionary * params = 
                             [NSMutableDictionary dictionaryWithObjectsAndKeys:
                             @"select birthday, name, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) order by name", 
                             @"query",
                             nil];
    
        [self.facebook requestWithMethodName: @"fql.query" andParams: params andHttpMethod: @"POST" andDelegate: self];
    }
    
    - (void) loginWithFacebook {
        self.facebook = [[[Facebook alloc] initWithAppId:@"<your app id here>" andDelegate:self] autorelease];
        //IMPORTANT - you need to ask for permission for friend's birthdays
        [facebook authorize:[NSArray arrayWithObject:@"friends_birthday"]];
    }
    

    【讨论】:

    • 您是否考虑接受我的回答,因为您接受了我对您其他相关帖子的回答?谢谢
    【解决方案2】:

    您应该使用此代码(我将其与 Hackbook 代码示例一起使用,并且效果很好):

    在 APICallsViewController.m 添加这些函数:

    /*
    /* Graph API: Method to get the user's friends.
    */
    - (void)apiGraphFriendsWithBirthdays {
          [self showActivityIndicator];
          HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
          NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
          @"picture,id,name,link,birthday,gender,last_name,first_name",@"fields",
     nil];
    
          [[delegate facebook] requestWithGraphPath:@"me/friends" andParams:params andHttpMethod:@"GET" andDelegate:self];
    

    }

    上面会获取大量数据,您只能使用 id、name 和生日...

    /*
    * Helper method to first get the user's friends and birthdays then
    * do something with it.
    */
    - (void)getFriendsForSetBirthday {
         // Call the friends Birthday API first
         currentAPICall = kAPIFriendsForSetBirthday;
         [self apiGraphFriendsWithBirthdays];
    }
    

    将此添加到案例部分的“- (void)request:(FBRequest *)request didLoad:(id)result”:

        case kAPIFriendsForSetBirthday:
        {
            NSMutableArray *friends = [[NSMutableArray alloc] initWithCapacity:1];
            NSArray *resultData = [result objectForKey:@"data"];
            if ([resultData count] > 0) {
                for (NSUInteger i=0; i<[resultData count] && i < 25; i++) {
                    [friends addObject:[resultData objectAtIndex:i]];
    
                    NSDictionary *friend = [resultData objectAtIndex:i];
                    long long fbid = [[friend objectForKey:@"id"]longLongValue];
                    NSString *name = [friend objectForKey:@"name"];
                    NSString *birthday = [friend objectForKey:@"birthday"];
                    NSLog(@"id: %lld - Name: %@ - Birthday: %@", fbid, name,birthday);
    
    
                }
    
            } else {
                [self showMessage:@"You have no friends."];
            }
            [friends release];
            break;
    
    
        }
    

    你需要申请生日阅读的权限(我是在点赞权限部分做的,但你可以在任何你喜欢的地方做,注意你必须先申请才能工作):

    - (void)apiPromptExtendedPermissions {
       currentAPICall = kDialogPermissionsExtended;
       HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
       NSArray *extendedPermissions = [[NSArray alloc] initWithObjects:@"user_likes",@"friends_birthday", nil];
       [[delegate facebook] authorize:extendedPermissions];
       [extendedPermissions release];
     }
    

    在 .h 文件中不要忘记为 apicall 添加 kAPIFriendsForSetBirthday。

    在 Dataset.m 上添加:

    NSDictionary *graphMenu7 = [[NSDictionary alloc] initWithObjectsAndKeys:
                                    @"Get friends birthday", @"title",
                                    @"You can get all friends birthdays.", @"description",
                                    @"Get friends birthday", @"button",
                                    @"getFriendsForSetBirthday", @"method",
                                    nil];
    

    并将graphMenu7添加到该文件的菜单中,不要忘记释放它。

    我已经测试过了,它工作得很好, 希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您可以将NSObject 子类化,将其称为“BirthdayLoader”并在那里实现请求。现在,在您的控制器中,只需创建这些加载器对象的实例来检索生日。当它们成功检索到生日时,它们可以使用委托方法进行报告。

      @protocol BirthDayLoaderDelegate 
      -(void)didFinishLoadingRequest:(FBRequest *)request withResult:(id)result;
      @end
      

      【讨论】:

        【解决方案4】:

        考虑到要获得生日(以及其他一些属性,例如用户简历),应用必须经过 Facebook 审核。否则,尽管权限已正确授予,但 Graph API 调用将不会检索此属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-07
          • 2012-08-22
          • 1970-01-01
          • 1970-01-01
          • 2015-01-24
          • 2016-04-30
          • 1970-01-01
          • 2014-03-13
          相关资源
          最近更新 更多