Last active
April 5, 2016 07:09
-
-
Save yonat/0a37ccb89adfc8f92d740e8549093d14 to your computer and use it in GitHub Desktop.
Convenience method for Facebook iOS SDK to page through all graph results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FBSDKGraphRequest+Paging.h | |
// Facebook Paging Extension | |
// | |
// Created by Yonat Sharon on 04.04.2016. | |
// Copyright © 2016 Yonat Sharon. All rights reserved. | |
// | |
#import <FBSDKCoreKit/FBSDKCoreKit.h> | |
@interface FBSDKGraphRequest (Paging) | |
/// Page through results, executing the completion block for each page of results. | |
- (void)startPagingWithCompletionHandler:(void (^)(id result, NSError *error))completion; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FBSDKGraphRequest+Paging.m | |
// Facebook Paging Extension | |
// | |
// Created by Yonat Sharon on 04.04.2016. | |
// Copyright © 2016 Yonat Sharon. All rights reserved. | |
// | |
#import "FBSDKGraphRequest+Paging.h" | |
@interface FBSDKGraphRequest (PagingPrivate) | |
- (void)pageJSON:(id)json completionHandler:(void (^)(id result, NSError *error))completion; | |
- (id)decodeJSONResult:(NSData *)data response:(NSURLResponse *)response error:(NSError *)error; | |
@end | |
@implementation FBSDKGraphRequest (Paging) | |
- (void)startPagingWithCompletionHandler:(void (^)(id result, NSError *error))completion { | |
[self startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { | |
if (error) { | |
NSLog(@"Error executing facebook request %@ info: %@", self.graphPath, error.localizedDescription); | |
completion(nil, error); | |
return; | |
}; | |
completion(result, nil); | |
[self pageJSON:result completionHandler:completion]; | |
}]; | |
} | |
@end | |
@implementation FBSDKGraphRequest (PagingPrivate) | |
- (void)pageJSON:(id)json completionHandler:(void (^)(id result, NSError *error))completion { | |
NSString *nextPage = json[@"paging"][@"next"]; | |
if (!nextPage) return; | |
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:nextPage] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { | |
id json = [self decodeJSONResult:data response:response error:error]; | |
if (!json) { | |
completion(nil, error); | |
return; | |
} | |
completion(json, nil); | |
[self pageJSON:json completionHandler:completion]; | |
}] resume]; | |
} | |
- (id)decodeJSONResult:(NSData *)data response:(NSURLResponse *)response error:(NSError *)error { | |
if (error) { | |
NSLog(@"Error executing facebook paging: %@", error.localizedDescription); | |
return nil; | |
}; | |
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; | |
if (httpResponse.statusCode != 200) { | |
NSLog(@"facebook paging request returned status: %d", (int)httpResponse.statusCode); | |
return nil; | |
} | |
NSError *jsonDecodingError = nil; | |
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonDecodingError]; | |
if (jsonDecodingError) { | |
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
if ([returnString isEqualToString:@"none"]) { | |
json = [NSDictionary dictionary]; | |
} | |
else { | |
NSLog(@"facebook paging request returned malformed JSON: %@", returnString); | |
return nil; | |
} | |
} | |
return json; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment