Sunday, November 19, 2017

How to add a share intent with robovm (libgdx)?

For iOS users:

RoboVM is deprecated in favor of RoboPods. So, RoboVM is no more, what now?
For sharing text using roboVM, you can set the UIAvtivityViewController class
NSString textShare = new NSString("This is the text to share");
NSArray texttoshare = new NSArray(textShare);
UIActivityViewController share = new UIActivityViewController(texttoshare,null);
presentViewController(share, true, null);
For sharing text, image and app, you can do the following code,
NSURL imgUrl = new NSURL(EXTERNAL_IMG_URL);
NSData data = NSData.read(imgUrl); 
UIImage image = new UIImage(data); 
NSString appStoreUrl = new NSString(APP_STORE_URL); 
NSString googleUrl = new NSString(GOOGLE_PLAY_URL); 
NSString text = new NSString(TEXT_TO_SHARE); 
NSArray<NSObject> texttoshare = new NSArray<NSObject>(text, image, appStoreUrl, googleUrl); 
UIActivityViewController share = new UIActivityViewController( 
texttoshare, null); 
if (UIDevice.getCurrentDevice().getUserInterfaceIdiom() == UIUserInterfaceIdiom.Phone) {  

 //iPhone 
iosApplication.getUIViewController().presentViewController( 
 share, true, null);  
} else { 
 //iPad 

 UIPopoverController popover = new UIPopoverController(share);
 UIView view = iosApplication.getUIViewController().getView(); 
 CGRect rect = new CGRect( 
 view.getFrame().getWidth() / 2, 
 view.getFrame().getHeight() / 4, 
 0, 
 0); 
 popover.presentFromRectInView( rect, view, UIPopoverArrowDirection.Any, true); 
 }
The following code sends an animated .gif through Mail and Messenger, however, Twitter only shares a static image.
public void shareGif(String path)
{        
    NSURL url = new NSURL(new File(path));

    NSArray<NSObject> imageArray = new NSArray<NSObject>(image);
    UIActivityViewController share = new UIActivityViewController(imageArray,null);
    ((IOSApplication)Gdx.app).getUIViewController().presentViewController(share, true, null);
}
For generic share in iOS, with text, link and image the code is given below:
@Override
public void share() {
    NSArray<NSObject> items = new NSArray<>(
            new NSString("Hey! Check out this mad search engine I use"),
            new NSURL("https://www.google.com"),
            new UIImage(Gdx.files.external("image.png").file())
    );
    UIActivityViewController uiActivityViewController = new UIActivityViewController(items, null);
    ((IOSApplication) Gdx.app).getUIViewController().presentViewController(uiActivityViewController, true, null);
}

For Android users: Sharing Content with intents

It says RoboVM is for iOS only, So how would we use it for android using LibGDX? roboVM with RoboPods is used for iOS while google play services is used for android. Different devices, different code. For android, after integrating the android sdk to your IDE, just link the google play services lib to the android project.

No comments:

Post a Comment