Keeping up with the theme of my last post, I’m here today sharing another single class solution to a common stack overflow question; How to share info (in this case a screenshot) to social media platform on your phone.
For our recent release, Pew Zoom Boom, we wanted a way for people to share scores, beyond just Leaderboards, so we implemented sharing for social media networks. It’s pretty simple to set up and get working in your own project.
Set Up
First you’ll need to save the screenshot to a path that’s accessible from your app. Then call this Method here:
[code language=”javascript”]
///
/// Shares an image using the preferred method in Android or iOS.
///
public void ShareImage(string path, string extratxt = “”)
{
}
[/code]
The path is to the screenshot you’ve taken, and the ‘extratxt’ is for any text you’d like to add to your post.
Sharing Is Caring
Now we need to fill in the empty method above. The Method is split by #if blocks for Android and iOS.
Android
For Android you want to setup a few things first:
- first set up the share intent,
- add the ‘extratxt’
- finally, create a Java File object and then get the uri of the tha Java File image.
[code language=”javascript”]
// Set up the Share Intent
var shareIntent = new Intent(Intent.ActionSend);
// Add Text to go along with it
shareIntent.PutExtra(Intent.ExtraText, extratxt);
// The link to the photo you want to share (i.e the path to the saved screenshot)
Java.IO.File photoFile = new Java.IO.File(path);
var uri = Android.Net.Uri.FromFile(photoFile);
[/code]
Then finally add the uri stream to the shareIntent, Set the Type to Image. You also need to App ReadURI Permissions.
[code language=”javascript”]
// Add the required info the the shareIntent
shareIntent.PutExtra(Intent.ExtraStream, uri);
shareIntent.SetType(“image/*”);
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
[/code]
And then finally launch the intent by calling the below code.
[code language=”javascript”]
// Now Send the Share Intent
Android.App.Application.Context.StartActivity(Intent.CreateChooser(shareIntent, “Choose one”));
[/code]
Calling the last line will start the share intent allowing you to share across different social media platforms:
iOS
iOS requires even less lines of code, but needs a special line if it’s for a tablet opposed to a phone.
[code language=”javascript”]
// Get the ViewController from MonoGame
UIViewController ViewController = Engine.Game.Services.GetService(typeof(UIViewController)) as UIViewController;
// Create an Image Object with the Path to the Screenshot
UIImage image = new UIImage(path);
NSObject[] shareItems = { image };
// Create a View Controller For the Activity
UIActivityViewController shareViewController = new UIActivityViewController(shareItems, null);
shareViewController.ExcludedActivityTypes = new NSString[] { };
// Handle if it’s for an iPad
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
shareViewController.PopoverPresentationController.SourceView = ViewController.View;
shareViewController.PopoverPresentationController.SourceRect = new CoreGraphics.CGRect((ViewController.View.Bounds.Width / 2), (ViewController.View.Bounds.Height / 4), 0, 0);
}
// Finally present the Activity
ViewController.PresentViewController(shareViewController, true, null);
[/code]
And that’s all, from there you can share to any social networks you have installed. Through my own testing it’s worked with Twitter, WhatsApp, Facebook, and Sanpchat.
Putting It All Together
We can put both of these blocks of code together into a single method seperated with #if blocks:
[code language=”javascript”]
///
/// Shares an image using the preferred method in Android or iOS.
///
/// Path.
public void ShareImage(string path, string extratxt = “”)
{
#if __ANDROID__
try
{
// Set up the Share Intent
var shareIntent = new Intent(Intent.ActionSend);
// Add Text to go along with it
shareIntent.PutExtra(Intent.ExtraText, extratxt);
// The link to the photo you want to share (i.e the path to the saved screenshot)
Java.IO.File photoFile = new Java.IO.File(path);
var uri = Android.Net.Uri.FromFile(photoFile);
// Add the required info the the shareIntent
shareIntent.PutExtra(Intent.ExtraStream, uri);
shareIntent.SetType(“image/*”);
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
// Now Send the Share Intent
Android.App.Application.Context.StartActivity(Intent.CreateChooser(shareIntent, “Choose one”));
}
catch
{
Console.WriteLine(“Error Sharing Results…”);
}
#elif __IOS__
try
{
//UIViewController ViewController = Engine.Game.Services.GetService(typeof(UIViewController)) as UIViewController;
UIImage image = new UIImage(path);
NSObject[] activityItems = { image };
UIActivityViewController activityViewController = new UIActivityViewController(activityItems, null);
activityViewController.ExcludedActivityTypes = new NSString[] { };
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
activityViewController.PopoverPresentationController.SourceView = ViewController.View;
activityViewController.PopoverPresentationController.SourceRect = new CoreGraphics.CGRect((ViewController.View.Bounds.Width / 2), (ViewController.View.Bounds.Height / 4), 0, 0);
}
ViewController.PresentViewController(activityViewController, true, null);
}
catch
{
Console.WriteLine(“Error Sharing Results…”);
}
#else
Console.WriteLine(“Sharing Not Available On This Platform.”);
#endif
}
[/code]
Pop it into your code and give it a try!
And If you like what you read, give me a follow here or keep up with me on Twitter.
Reblogged this on rtdev2.