0

When I run my ios app which integrates a facebook share, the launcher screen appears and it goes back to the view controller. It does not execute the codes that displays Share Dialog

this is my code and i am not using the latest version of Facebook SDK

- (IBAction)buttonShare:(id)sender {
    [self facebookPost];
}

-(void) facebookPost{
    NSArray *permissions = [NSArray arrayWithObjects:@"email", @"publish_actions", @"user_friends", @"public_profile", nil];

    [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {

        if(!error) {

            if(status == FBSessionStateOpen || status == FBSessionStateOpenTokenExtended) {

                [FBSession setActiveSession:session];

                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                               self.model.name, @"name",
                                               self.model.categoryName, @"caption",
                                               self.model.address, @"description",
                                               self.model.imgURL, @"link", nil];

                [FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {

                    if (error) {

                        NSLog(@"Error publishing story: %@", error.description);

                    } else {

                        if (result == FBWebDialogResultDialogNotCompleted) {
                            // User cancelled.
                            NSLog(@"User cancelled.");

                        } else {
                            // Handle the publish feed callback
                            NSDictionary *urlParams = [self parseURLParams:[resultURL query]];

                            if (![urlParams valueForKey:@"post_id"]) {
                                // User cancelled.
                                NSLog(@"User cancelled.");

                            } else {
                                // User clicked the Share button
                                NSString *result = [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]];

                                NSLog(@"result %@", result);
                            }
                        }
                    }
                }];
            }
        }
        else{
            NSLog(@"Error");
        }
    }];

}

- (NSDictionary*)parseURLParams:(NSString *)query {
    NSArray *pairs = [query componentsSeparatedByString:@"&"];

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

    for (NSString *pair in pairs) {

        NSArray *kv = [pair componentsSeparatedByString:@"="];
        NSString *val = [kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        params[kv[0]] = val;
    }
    return params;
}
rebello95
  • 8,486
  • 5
  • 44
  • 65
thedansaps
  • 611
  • 3
  • 10
  • 18

1 Answers1

0

1. For sharing links :

if ([FBDialogs canPresentShareDialogWithParams:nil]) {

    NSURL* url = [NSURL URLWithString:link.url];
    [FBDialogs presentShareDialogWithLink:url
                              handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                 if(error) {
                       NSLog(@"Error: %@", error.description);
                 } else {
                       NSLog(@"Success");
                 }
     }];
}

2.For OpenGraph calls :

id<FBGraphObject> pictureObject =
    [FBGraphObject openGraphObjectForPostWithType:@"your_namespace:picture"
                                            title:image.title
                                            image:image.thumbnailUrl
                                              url:image.url
                                      description:@""];

    id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
    [action setObject:pictureObject forKey:@"picture"];

    [FBDialogs presentShareDialogWithOpenGraphAction:action
                                          actionType:@"your_namespace:action_name"
                                 previewPropertyName:@"picture"
                                             handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                 if(error) {
                                                     NSLog(@"Error: %@", error.description);
                                                 } else {
                                                     NSLog(@"Success");
                                                 }
                                             }];

Check out other ways to share on iOS here

3.Another way using SLComposeViewController..

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [mySLComposerSheet setInitialText:@"iOS 6 Social Framework test!"];

        [mySLComposerSheet addImage:[UIImage imageNamed:@"myImage.png"]];

        [mySLComposerSheet addURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing"]];

        [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {

             switch (result) {
                 case SLComposeViewControllerResultCancelled:
                     NSLog(@"Post Canceled");
                     break;
                 case SLComposeViewControllerResultDone:
                     NSLog(@"Post Sucessful");
                     break;

                 default:
                     break;
             }
         }];

        [self presentViewController:mySLComposerSheet animated:YES completion:nil];
    }
Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
  • Rahul, I am a beginner and I am not using the latest version of Facebook SDK. That code above works for a simple exercise I have. – thedansaps Apr 17 '15 at 05:21
  • See this url : http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing – Rahul Mayani Apr 17 '15 at 05:29
  • so given in my above code, where will i place the third solution? – thedansaps Apr 17 '15 at 05:44
  • but my previous app is functioning well using the above code and not using the latest facebook sdk version, how come that i always get back to the view controller and the facebook share dialog is not working – thedansaps Apr 17 '15 at 05:55