8

In Facebook iOS SDK, I can ask for queries like this:

[_facebook requestWithGraphPath:@"me/feed" andDelegate:self];

But often Facebook will give a limited JSON response with a URL to be used to request to move to earlier dates, for example. So in the JSON response, I'll have:

data =     ( /*things here... status updates, photos, etc...*/
    );
paging =     {
        next = "https://graph.facebook.com/me/feed?sdk=ios&sdk_version=2&access_token= <something>&until=2010-12-04";
        previous = "https://graph.facebook.com/me/feed?sdk=ios&sdk_version=2&access_token=<something>&since=<something>";
    };

What I'm wondering is... How do I go to the previous URL? Does the SDK provide an interface to do this?

EDIT: If possible, I actually want answer with Graph API, as Facebook is currently deprecating the REST API.

BONUS: If anyone can explain the time format that's returned by Facebook. I have 2010-09-13T00%3A25%3A16%2B0000 as an example.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • 3
    By the way, your time format is just URL Encoded. Decode it and you get this "2010-09-13T00:25:16+0000". More obvious now? :) – DougW Aug 31 '12 at 23:02
  • Yes! I didn't know how I've missed that. It's been a year and a half since I posted this. – Enrico Susatyo Sep 02 '12 at 05:54

5 Answers5

7

all what you need that add a method to the Facebook subclass or itself

- (void)requestWithURLString:(NSString *)fullURL
               andHttpMethod:(NSString *)httpMethod
                 andDelegate:(id <FBRequestDelegate>)delegate {
    [self openUrl:fullURL params:nil httpMethod:httpMethod delegate:delegate];
}

ps the second param "httpMethod" may be always @"GET" you can omit it

UIBuilder
  • 1,333
  • 12
  • 15
  • WOW, works very well! Thank you very much! BTW why isn't Facebook SDK build this in? Do you know if it's a planned update? – Enrico Susatyo Feb 08 '11 at 00:43
  • No, I didn`t find any tasks about that feature at https://github.com/facebook/facebook-ios-sdk/issues but You can be first who done it ) – UIBuilder Feb 08 '11 at 09:30
  • This answer no longer applies to the latest version of the Facebook SDK. I've added a new answer explaining how to accomplish it in the latest version. – DougW Aug 31 '12 at 22:46
6

With Facebook's iOS SDK version 3 out, the original answer no longer applies to the current version.

I had to do some digging, because the new version doesn't make this any easier. I found an example of how to get this done in the FBGraphObjectPagingLoader class that the new SDK provides to help do this for tables. It's incredibly ugly, but I assume that it's the "recommended" method since it's what they use.

Here's my slight modification of their code (found originally in FBGraphObjectPagingLoader's followNextLink method)

FBRequest *request = [[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:nil] autorelease];
FBRequestConnection *connection = [[[FBRequestConnection alloc] init] autorelease];
[connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
     // Do some stuff
 }];

// Override the URL using the one passed back in 'next'.
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
connection.urlRequest = urlRequest;

[connection start];

You could of course modify their library and encapsulate this in the class itself if you wanted to.

Ahmad Baraka
  • 1,093
  • 9
  • 12
DougW
  • 28,776
  • 18
  • 79
  • 107
2

You can do something like this:

[appDelegate.fb requestWithGraphPath:@"me/home"
                           andParams:[NSMutableDictionary dictionaryWithObject:@"2011-01-27T04%3A48%3A50%2B0000" forKey:@"since"]
                         andDelegate:self];

Notice that, in the paging portion of your feed, the next and previous URLs differ just by one query parameter (until and since). You can use the values you grab from this to get the next and previous page of results.

Hope this helps!

donkim
  • 13,119
  • 3
  • 42
  • 47
  • Hmm not working for me, I have an error from facebook: could not parse 2011-01-27T04%3A48%3A50%2B0000 into a date or time. Any other ideas? – Enrico Susatyo Feb 07 '11 at 01:49
  • Hmm, that's weird... It worked for me before. I'll try to figure this out too, as I need it for a project of mine.. Will keep you updated.. – donkim Feb 07 '11 at 04:37
  • Search for "since and until" on this page: https://developers.facebook.com/docs/api. It says you can use "almost any valid date format." I've tried using "since=yesterday" and "until=yesterday" and they both work. I also tried using a timestamp (converted here: http://www.epochconverter.com/) and that also gave me a valid time format. So... it looks like you can convert it to a Unix timestamp. Though I think it's weird that they'd offer the date in the format above and not accept it... Needs more investigating.. – donkim Feb 07 '11 at 04:44
  • I actually don't quite understand the time format. The one that I'm testing is 2010-09-13T00%3A25%3A16%2B0000, when I tried epochconverter.com it says that it's 1st Jan 1970. Something must definitely be wrong in here... I'll investigate a little bit more as well. – Enrico Susatyo Feb 07 '11 at 05:46
  • @donkim It seems that Gaj has done it. Although I won't say it's a particularly elegant solution (hacking the SDK), I'll see if Facebook has this method in the pipeline. – Enrico Susatyo Feb 08 '11 at 00:48
  • That's a great solution. I actually *just* got it to work with the solution above (you have to use the `until` parameter, extract the date, then put it in the dictionary using `[NSString stringWithFormat:@"%@" extractedDate]`. But you know, I like that solution and I agree that it should be included in the SDK. – donkim Feb 08 '11 at 02:35
1

Yes you can get the result by calling the function in api I used below code to get the statuses of users in your case you can use stream.get method you can found it here http://developers.facebook.com/docs/reference/rest/stream.get/

NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
    [params setValue:[NSString stringWithFormat:@"%@", appDelegate.user_id] forKey:@"uid"];
    [params setValue:@"150" forKey:@"limit"];
    [params setValue:@"results" forKey:@"callback"];


    [_facebook requestWithMethodName: @"status.get"
       andParams: params
       andHttpMethod: @"POST"
                         andDelegate: self];

You can use this code for you purpose.

Inam Abbas
  • 1,480
  • 14
  • 28
0

At least as far as the date format goes, its a variant of RFC 3339 format. As far as I know, iOS doesn't have a pre-defined formatter of that type.

I create a date formatter and keep it around (they're are strangely slow to create) so I can easily convert them when working with FB data.

        NSDateFormatter * sRFC3339DateFormatter = nil;
        NSLocale * enUSPOSIXLocale;

        sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
        enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];

        [sRFC3339DateFormatter setLocale:enUSPOSIXLocale];
        [sRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
        [sRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Once you have such a formatter, you can easily convert back and forth.

NSDate* myDate = nil;
myDate = [sRFC3339DateFormatter dateFromString:@"2011-01-27T04%3A48%3A50%2B0000"];

Breaking the URL up into a dictionary of strings is pretty straightforward with NSURL methods.

giff
  • 1,720
  • 1
  • 14
  • 21