iosobjective-cfacebookfacebook-graph-apislrequest

Attaching a URL to a facebook post using SLRequest?


I'm successfully able to post to facebook using SLRequest, in the following way:

- (void)postScoreWithFBAccount:(ACAccount *)facebookAccount
{

    NSString *messageToPost = [self.myString stringByAppendingString:@"https://www.example.com"];

    NSDictionary *fbParameters = @{@"message": messageToPost};
    NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

    SLRequest *updateFeedRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:feedURL parameters:fbParameters];
    updateFeedRequest.account = facebookAccount;
    [updateFeedRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if (responseData )
         {
             NSLog(@"Received Response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
             if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300)
                 {
                 }
             else
                 {
                     NSLog(@"HTTP %d Returned", urlResponse.statusCode);
                 }
         }
         else
         {
             [self showErrorAlertWithMessage:@"Unknown error occured, try again later!"];
         }
     }];
}

I thought I could simply attach a url by appending one to my message string. The problem is that even though it is recognized as a link on facebook, it doesn't show a preview when broswing on facebook, like other url's do. Does anyone know how I can attach a url similar to how the SLComposerViewController does it, but instead through SLRequest?


Solution

  • You can add another parameter to the /feed: link.

    See here for the complete list of parameters.

    Edit:

    You can also add some og:tags in your url inside <head> that facebook will crawl and show by default in the feed:

    <meta property="og:title" content="{your title here}" />
    <meta property="og:description" content="{your description here}" />
    <meta property="og:image" content="{your image here}" />
    

    You can check what is facebook crawling from your site, here: Debugger