不与SLComposeViewController连接 - 不与Facebook共享屏幕截图

时间:2015-05-21 18:24:02

标签: ios objective-c facebook

我在Facebook上分享我的应用程序截图的代码总是显示alertView,从不在Facebook上发布:

- (UIImage *) screenshot
{
    AppDelegate* app = (((AppDelegate*)[UIApplication sharedApplication].delegate));
    UIGraphicsBeginImageContextWithOptions(app.navController.view.bounds.size, NO, [UIScreen mainScreen].scale);

    [app.navController.view drawViewHierarchyInRect:app.navController.view.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(void)btnSharedFacebookTapped:(id)sender
{
    [[AudioManager sharedAudioManager]playSoundEffect:kSoundGrilloMenu];

    // Take screenshot
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        AppDelegate* app = (((AppDelegate*)[UIApplication sharedApplication].delegate));
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
        {
            if (result == SLComposeViewControllerResultCancelled)
                {
                    NSLog(@"Cancelled");
                } else {
                    NSLog(@"Done");
            }

        [app.navController dismissViewControllerAnimated:YES completion:Nil];
        };

    controller.completionHandler =myBlock;

    //Adding the Text to the facebook post value from iOS
    [controller setInitialText:@"Checkout this app xxxxxx”];
    [controller addImage:[self screenshot]];

    //Adding the URL to the facebook post value from iOS
    [controller addURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/xxxxxxx/id[xxx]?mt=8‎"]];


    [app.navController presentViewController:controller animated:YES completion:Nil];

    } else{
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry"
                                                                message:@"You can't send a post right now, make sure your device has an internet connection and you have at least one Facebook account setup."
                                                               delegate:self
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
            [alertView show];
      }
}

1 个答案:

答案 0 :(得分:0)

我为此创建了一个演示APP。在这个APP,我发布状态和上传照片,但我从来没有失败错误。

- (IBAction)btnSocialSharing:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Social Media"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Facebook", @"Twitter" ,nil];
    // actionSheet.tag = 100;
    [actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
   // NSString *image=[UIImage imageNamed:chooseImage];
    if ([buttonTitle isEqualToString:@"Facebook"])
    {
        
        SLComposeViewController *controller = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeFacebook];
        SLComposeViewControllerCompletionHandler myBlock =
        ^(SLComposeViewControllerResult result){
            if (result == SLComposeViewControllerResultCancelled)
            {
                NSLog(@"Cancelled");
            }
            else
            {
                NSLog(@"Done");
            }
            [controller dismissViewControllerAnimated:YES completion:nil];
        };
        controller.completionHandler =myBlock;
        [controller setInitialText:self.txtStatus.text];
        [controller addImage:chooseImage];
        [self presentViewController:controller animated:YES completion:nil];
        
    }
   else if ([buttonTitle isEqualToString:@"Twitter"])
    {
        
        SLComposeViewController *controller = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        SLComposeViewControllerCompletionHandler myBlock =
        ^(SLComposeViewControllerResult result){
            if (result == SLComposeViewControllerResultCancelled)
            {
                NSLog(@"Cancelled");
            }
            else
            {
                NSLog(@"Done");
            }
            [controller dismissViewControllerAnimated:YES completion:nil];
        };
        controller.completionHandler =myBlock;
        [controller setInitialText:self.txtStatus.text];
        [controller addImage:chooseImage];
        [self presentViewController:controller animated:YES completion:nil];
        
    }
}



- (IBAction)btnSelectPhoto:(id)sender {
    NSLog(@"Select Photos button");
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:NULL];


}
//Image Picker Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   
    
    chooseImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
    [self displayImage];
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
}
-(void)displayImage
{
    self.viewImage.image = chooseImage;
}

enter image description here