通过Facebook共享应用程序无法正常工作

时间:2014-07-14 09:27:40

标签: ios objective-c facebook facebook-graph-api

您好,在我的应用程序中,我可以选择通过Facebook共享我的应用程序。所以我已经将Facebook framework添加到我的项目中,并且我已将包ID添加到我的Facebook中并获得了应用程序ID,但问题是我无法共享其显示的应用程序。

An error occurred. Please try again later

在我的刺激器中显示的是这样的,我不知道为什么它会这样显示。如果我的设备的Facebook App工作正常,但如果它没有Facebook App显示错误,请执行此操作。请告诉我们如何解决此问题。

   - (IBAction)share:(id)sender {
     FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
     params.link = [NSURL URLWithString:@"url/"];

// If the Facebook app is installed and we can present the share dialog
 if ([FBDialogs canPresentShareDialogWithParams:params]) {

    // Present share dialog
     [FBDialogs presentShareDialogWithLink:params.link
                                  handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                      if(error) {
                                          // An error occurred, we need to handle the error
                                          // See: https://developers.facebook.com/docs/ios/errors
                                          NSLog(@"Error publishing story: %@", error.description);
                                      } else {
                                          // Success
                                          NSLog(@"result %@", results);
                                      }
                                  }];

    // If the Facebook app is NOT installed and we can't present the share dialog
  } else {
    // FALLBACK: publish just a link using the Feed dialog

    // Put together the dialog parameters
    // Put together the dialog parameters
     NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"My appname", @"name",
                                   @"test share.", @"caption",
                                   @"sample.", @"description",
                                   @"myrul", @"link",

                                   nil];

    // Show the feed dialog
      [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                           parameters:params
                                              handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                                  if (error) {
                                                      // An error occurred, we need to handle the error
                                                      // See: https://developers.facebook.com/docs/ios/errors
                                                      NSLog(@"Error publishing story: %@", error.description);
                                                  } else {
                                                      if (result == FBWebDialogResultDialogNotCompleted) {
                                                          // User canceled.
                                                          NSLog(@"User cancelled.");
                                                      } else {
                                                          // Handle the publish feed callback
                                                          NSDictionary *urlParams = [self parseURLParams:[resultURL query]];

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

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

                                       }
                                     }


    - (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;
   }

我使用上面的代码不能正常工作请帮我解决这个问题。

感谢。

1 个答案:

答案 0 :(得分:0)

将此代码用于Share App。

-(void)facebook
{

   if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {


        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];


        SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
            if (result == SLComposeViewControllerResultCancelled) {

                NSLog(@"Cancelled");

            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congratulations!" message:@"Successfully posted to facebook Wall." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

                [alert show];

                [Flurry logEvent:@"Share screen - Successfully posted on Facebook"];
            }

            [controller dismissViewControllerAnimated:YES completion:Nil];
        };

        controller.completionHandler =myBlock;
       NSString *str=[NSString stringWithFormat:@" I am using App for my iPhone. You too can download it from here"];
        [controller setInitialText:str];
        [self presentViewController:controller animated:YES completion:Nil];
    }
    else{
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!"message:@"Please add Facebook account in settings menu" delegate:self cancelButtonTitle:Nil otherButtonTitles:@"Ok", nil];
          alert.tag=100;
         [alert show];
    }
}

要使用 SLComposeViewController ,您必须添加 社交框架 。它从设置获取您的Facebook帐户,您可以通过 SLComposeViewController 分享您的应用程序链接。和

如果您要将图片或网址添加到:

[composeController addImage:[UIImage imageNamed:@"image.png"]]; 
[composeController addURL: [NSURL URLWithString:@"apple.com"]];