带有Facebook Share的IOS App回到视图控制器

时间:2015-04-17 05:11:44

标签: ios objective-c xcode

当我运行集成了facebook共享的ios应用程序时,会出现启动器屏幕并返回到视图控制器。它不执行显示Share Dialog

的代码

这是我的代码,我没有使用最新版本的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;
}

1 个答案:

答案 0 :(得分:0)

<强> 1。用于分享链接:

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.对于OpenGraph调用:

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");
                                                 }
                                             }];

查看在iOS here

上分享的其他方式

3.使用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];
    }