Facebook Graph API - get a photos album ID

后端 未结 9 2228
小蘑菇
小蘑菇 2020-12-31 09:32

I\'m writing an application that uploads a photo to facebook. I\'m not supplying an album ID so an album is created on behalf of my app.

My question is: how do I get

9条回答
  •  北海茫月
    2020-12-31 10:06

    This issue has been bugging me for a while as well, but I think I have cracked the nut.

    The album ID is hidden in the link value, so if you have a photo-post object from the Graph API, you can retrieve the Album ID like this:

    NSString *link = [item valueForKey:@"link"];
    NSString *album_id = nil;
    NSRange fRange = [link rangeOfString:@"set=a."];
    if(fRange.location != NSNotFound) {         
        NSInteger firstPos = fRange.location + 6;
        NSInteger endPos = [link rangeOfString:@"." options:NSLiteralSearch range:NSMakeRange(firstPos, 25)].location;
        album_id = [[link substringWithRange:NSMakeRange(firstPos, endPos - firstPos)] copy];
    }
    

    It could probably all be stripped down to one line of code, but I think this is more readable. For getting the album information use:

    [facebook requestWithGraphPath:album_id andDelegate:delegate];
    

    For getting all the photos in an album use:

    [facebook requestWithGraphPath:[NSString stringWithFormat:@"%@/photos", album_id] andDelegate:delegate];
    

    Hope this can help someone.

提交回复
热议问题