Add the following dependency to your build.gradle
:
dependencies {
... other dependencies ...
compile "com.mobidevelop.robovm:robopods-facebook-ios-share:$robopodsVersion"
}
Add the following dependency to your pom.xml
:
<dependency>
<groupId>com.mobidevelop.robovm</groupId>
<artifactId>robopods-facebook-ios-share</artifactId>
<version>${robopods.version}</version>
</dependency>
Before you can share anything, you have to model the content you want to share.
To share links create an instance of FBSDKShareLinkContent
and specify the url:
FBSDKShareLinkContent content = new FBSDKShareLinkContent();
content.setContentURL(new NSURL("https://robovm.com"));
content.setContentTitle("RoboVM");
content.setContentDescription("Cross platform mobile app development in Java");
To share photos create an instance of FBSDKSharePhoto
with an UIImage
and pass it to a FBSDKSharePhotoContent
instance:
FBSDKSharePhoto photo = new FBSDKSharePhoto();
photo.setImage(UIImage.getImage("robovm-icon.png"));
photo.setUserGenerated(false);
FBSDKSharePhotoContent content = new FBSDKSharePhotoContent();
content.setPhotos(new NSArray<>(photo));
...
To share videos create an instance of FBSDKShareVideo
with an NSURL
video url and pass it to a FBSDKSharePhotoCFBSDKShareVideoContentontent
instance:
FBSDKShareVideo video = new FBSDKShareVideo();
video.setVideoURL(new NSURL("https://www.youtube.com/watch?v=wwr3sMo-84s"));
FBSDKShareVideoContent content = new FBSDKShareVideoContent();
content.setVideo(video);
After creating the share content, it's time to share them.
The FBSDKShareButton
will open the Facebook Share Dialog to share the specified content:
FBSDKShareButton button = new FBSDKShareButton();
button.setShareContent(content);
Alternatively you can use the FBSDKSendButton
which will share the content with the Messenger app.
FBSDKSendButton button = new FBSDKSendButton();
button.setShareContent(content);
Add the button as a subview to your view hierarchy:
view.addSubview(button);
If you want to directly show the share dialog use the following code:
FBSDKShareDialog.show(viewController, content, null);
You have to pass a UIViewController
when showing the dialog. Ideally you should display
the dialog in a view controller subclass action and specify this
.
If you're developing a libGDX game you can just get the root view controller of the application:
UIViewController viewController = UIApplication.getSharedApplication().getKeyWindow().getRootViewController();
Alternatively you can use the FBSDKMessageDialog
to share with the Messenger app.
FBSDKMessageDialog.show(content, null);
In the last parameter of both show
methods you can specify a delegate to get notified on share success, cancel and error.
You can also use your own UI for sharing. When you are about to share the content use the FBSDKShareAPI
:
FBSDKShareAPI.share(content, delegate);
Add code to share with Facebook and run your app. You should see the share dialog. If you implemented the share delegate you should also get notified whether the share was successful.
Note: You do not need to implement Facebook login to make sharing work!