1

I have the following code to post to Facebook for my app;

This is the .h file

#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import <Accounts/Accounts.h>

@interface LifeTipsViewController : UIViewController {
    SLComposeViewController *mySLComposerSheet;
}

  -(IBAction)PostToFacebook:(id)sender;

@end

and the .m file (Facebook posting code)

-(IBAction)PostToFacebook:(id)sender {
    mySLComposerSheet = [[SLComposeViewController alloc] init];
    mySLComposerSheet = [SLComposeViewController
    composeViewControllerForServiceType:SLServiceTypeFacebook];
    [mySLComposerSheet setInitialText:@"hi"];
    [self presentViewController:mySLComposerSheet animated:YES completion:NULL];
}

I want to be able to press the 'Share' button and the app to get a screen shot of the current view to post to Facebook. What code would need to be added to this Facebook posting code to allow it to share a screen shot of the app? Is this even possible?

Thanks in advance.

1 Answers1

2

Something like this.

-(IBAction)PostToFacebook:(id)sender {
    mySLComposerSheet = [[SLComposeViewController alloc] init];
    mySLComposerSheet = [SLComposeViewController
    composeViewControllerForServiceType:SLServiceTypeFacebook];
    [mySLComposerSheet setInitialText:@"hi"];


    UIGraphicsBeginImageContext(self.window.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [mySLComposerSheet addImage:image];


    [self presentViewController:mySLComposerSheet animated:YES completion:NULL];
}
Isaiah Turner
  • 2,574
  • 1
  • 22
  • 35
  • If it doesn't work blame https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/SLComposeViewController/addImage: and http://stackoverflow.com/questions/2200736/how-to-take-a-screenshot-programmatically but if it does, you should really learn to use Google. That is all I did. – Isaiah Turner Mar 04 '14 at 04:19
  • Thanks I'll try it when I get home. Had googled and tried a lot of things so turned to writing my own question. Didn't see the code you posted come up. – user3230481 Mar 04 '14 at 04:36