hanumanDev
hanumanDev

Reputation: 6614

How to use the Facebook SLComposeViewController in a UIView instead of a UIViewController

I'm having a problem using the Facebook SLComposeViewController in a UIView. The problem I'm having is because I have a UIView and not a UIViewController that I'm inheriting from.

I've imported:

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

and am trying to present the Facebook sharing using:

 SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:@"First post from my iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:@"http://www.test.com"]];
[controllerSLC addImage:[UIImage imageNamed:@"test.jpg"]];
[self presentViewController:controllerSLC animated:YES completion:Nil];

It's the last line it won't present the view because I'm inheriting from UIView and not UIVIewController I'm not sure how to fix this. I just want to share an image.

thanks for any help.

Upvotes: 0

Views: 1231

Answers (1)

Sean Kladek
Sean Kladek

Reputation: 4446

UIViews cannot present view controllers. You'll need to set up a way to call back to the UIViewController that is presenting to your UIView.

A common way would be to make create a delegate on your view. When something happens on that view, the delegate method is called which messages the view controller that implements your delegate methods. From there, you can present the view controller from your parent view controller.

In your view's .h file:

@protocol YourUIViewDelegate <NSObject>
- (void)theDelegateMethod;
@end

@interface YourUIView : UIView
@property (assign, nonatomic) id<YourUIViewDelegate> delegate;

In your view's .m file (I'm assuming there's a button press or some other action method that gets called where you want to present this view controller):

- (void)buttonTapped:(id)sender
{
    [self.delegate theDelegateMethod];
}

In the view controller .h that is presenting the delegate method:

@interface ThePresentingViewController <YourUIViewDelegate>

In that view controller's .m file:

- (void)theDelegateMethod
{
    // All of the SLComposeViewController code
}

Don't forget to set the view's delegate to the view controller within the presenting view controller.

Upvotes: 2

Related Questions