iOS Facebook Graph API使用"下一页"或"上一页"Url进行分页使用SDK

TWr*_*ght 4 facebook-graph-api ios facebook-ios-sdk

当结果被分页时,我不太确定想要利用图形api返回的"下一个"或"前一个"URL的最佳方法或正确的SDK调用.我已经查看了FBRequest和FBRequestConnection的文档,但是没有任何方法或调用可以作为我问题的明显解决方案.任何人都有一个或可以提出一个能指出正确方向的建议吗?

msm*_*smq 9

尼古拉的解决方案非常完美.这是它的Swift版本

func makeFBRequestToPath(aPath:String, withParameters:Dictionary<String, AnyObject>, success successBlock: (Array<AnyObject>?) -> (), failure failureBlock: (NSError?) -> ())
{
    //create array to store results of multiple requests
    let recievedDataStorage:Array<AnyObject> = Array<AnyObject>()

    //run requests with array to store results in
    p_requestFromPath(aPath, parameters: withParameters, storage: recievedDataStorage, success: successBlock, failure: failureBlock)
}


func p_requestFromPath(path:String, parameters params:Dictionary<String, AnyObject>, var storage friends:Array<AnyObject>, success successBlock: (Array<AnyObject>?) -> (), failure failureBlock: (NSError?) -> ())
{
    //create requests with needed parameters


    let req = FBSDKGraphRequest(graphPath: path, parameters: params, tokenString: FBSDKAccessToken.currentAccessToken().tokenString, version: nil, HTTPMethod: "GET")
    req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
        if(error == nil)
        {
            print("result \(result)")

            let result:Dictionary<String, AnyObject> = result as! Dictionary<String, AnyObject>


            //add recieved data to array
            friends.append(result["data"]!)
            //then get parameters of link for the next page of data

            let nextCursor:String? = result["paging"]!["next"]! as? String

            if let _ = nextCursor
            {
                let paramsOfNextPage:Dictionary = FBSDKUtility.dictionaryWithQueryString(nextCursor!)

                if paramsOfNextPage.keys.count > 0
                {
                    self.p_requestFromPath(path, parameters: paramsOfNextPage as! Dictionary<String, AnyObject>, storage: friends, success:successBlock, failure: failureBlock)
                    //just exit out of the method body if next link was found
                    return
                }
            }

            successBlock(friends)
        }
        else
        {
            //if error pass it in a failure block and exit out of method
            print("error \(error)")
            failureBlock(error)
        }
    })
}


func getFBFriendsList()
{
    //For example retrieve friends list with limit of retrieving data items per request equal to 5
    let anyParametersYouWant:Dictionary = ["limit":2]
    makeFBRequestToPath("/me/friends/", withParameters: anyParametersYouWant, success: { (results:Array<AnyObject>?) -> () in
        print("Found friends are: \(results)")
        }) { (error:NSError?) -> () in
            print("Oops! Something went wrong \(error)")
    }
}
Run Code Online (Sandbox Code Playgroud)


Nik*_*kov 8

要获得下一个链接,您必须这样做:

我用这种方式解决了这个问题:

添加标题

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
Run Code Online (Sandbox Code Playgroud)

和代码

//use this general method with any parameters you want. All requests will be handled correctly

- (void)makeFBRequestToPath:(NSString *)aPath withParameters:(NSDictionary *)parameters success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure
{
    //create array to store results of multiple requests
    NSMutableArray *recievedDataStorage = [NSMutableArray new];

    //run requests with array to store results in
    [self p_requestFriendsFromPath:aPath
                        parameters:parameters
                           storage:recievedDataStorage
                            succes:success
                           failure:failure];
}




 - (void)p_requestFromPath:(NSString *)path parameters:(NSDictionary *)params storage:(NSMutableArray *)friends succes:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure
{
    //create requests with needed parameters
        FBSDKGraphRequest *fbRequest = [[FBSDKGraphRequest alloc]initWithGraphPath:path
                                                                         parameters:params
                                                                         HTTPMethod:nil];

    //then make a Facebook connection
    FBSDKGraphRequestConnection *connection = [FBSDKGraphRequestConnection new];
    [connection addRequest:fbRequest
         completionHandler:^(FBSDKGraphRequestConnection *connection, NSDictionary*result, NSError *error) {

             //if error pass it in a failure block and exit out of method
             if (error){
                 if(failure){
                    failure(error);
                 }
                 return ;
             }
             //add recieved data to array
             [friends addObjectsFromArray:result[@"data"];
             //then get parameters of link for the next page of data
             NSDictionary *paramsOfNextPage = [FBSDKUtility dictionaryWithQueryString:result[@"paging"][@"next"]];
             if (paramsOfNextPage.allKeys.count > 0){
                 [self p_requestFromPath:path
                              parameters:paramsOfNextPage
                                 storage:friends
                                  succes:success
                                 failure:failure];
                 //just exit out of the method body if next link was found
                 return;
             }
             if (success){
                success([friends copy]);
             }
         }];
     //do not forget to run connection
    [connection start];
}
Run Code Online (Sandbox Code Playgroud)

如何使用:

得到朋友列表使用技术如下:

//For example retrieve friends list with limit of retrieving data items per request equal to 5

NSDictionary *anyParametersYouWant = @{@"limit":@5};
[self makeFBRequestToPath:@"me/taggable_friends/"
           withParameters:anyParametersYouWant
                  success:^(NSArray *results) {
                      NSLog(@"Found friends are:\n%@",results);    
                  }
                  failure:^[(NSError *) {
                      NSLog(@"Oops! Something went wrong(\n%@",error);
                  }];
];
Run Code Online (Sandbox Code Playgroud)