Since the Facebook SDK removed the ability to pre-provide a message to post to a users wall, Twitter has been our only way of easily allowing people to share with their friends whether or not it was a ‘shorts’ day from our iOS weather app Shorts or Not. But with the quirky style of our app and the rise in use of Instagram I thought I would add in the ability to share a screenshot via their API.
![]()
For taking the screenshot there is a useful function which has been posted on the Cocos2d forums here but you could post any UIImage using the same technique.
UIImage * screenshot = [[CCDirector sharedDirector] screenshotUIImage]; NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Screenshot.igo"]; // Write image to JPEG [UIImageJPEGRepresentation(screenshot, 1.0) writeToFile:savePath atomically:YES];
The next step is then to set the save path. We want this to open in Instagram exclusively so the extension should be ‘.igo’ ideally but you can also use ‘.ig’. The image should also be saved in JPEG or PNG format. Once we have written the image to the device we can then use the Instagram iPhone hooks, for more information on these go here. For the purposes of this application we only need to use the default app hook to open up the app.
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
We create the URL and then check that it can be opened, i.e the Instagram app is actually available on the device. If it is then we can continue and uses Apple’s UIDocumentInteractionController class. This class can be used to open documents in matching programs that are on a users iOS device, the UTI variable defines the type of prgrams we want to open. Because we want our code to only use Instagram we pass it the UTI of ‘com.instagram.exclusivegram’
//imageToUpload is a file path with .ig file extension documentInteractionController = [[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]]retain]; documentInteractionController.UTI = @"com.instagram.exclusivegram"; documentInteractionController.delegate = self;
You can also pass in a default caption by adding text into the annotation variable, for example with Shorts or Not we post whether its shorts or not weather today. The annotation value is an NSDictionary with the key ‘InstagramCaption’.
if(shortsFlag)
documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"Today is a Shorts day! Try Shorts Or Not for where you are - http://bit.ly/iDYwYC via @albino_pixel" forKey:@"InstagramCaption"];
else
documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"Today is not a Shorts day! Try Shorts Or Not for where you are - http://bit.ly/iDYwYC via @albino_pixel" forKey:@"InstagramCaption"];
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:[self appController].view.window animated:YES];
And then finally to present the menu of available apps we use the presentOpenInMenuFromRect call, which will give you a screen like below.
![]()
So the full function is as follows, and is called when ever the user presses our Instagram button.
-(void) instagramPressed:(id) sender
{
UIImage * screenshot = [[CCDirector sharedDirector] screenshotUIImage];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Screenshot.igo"];
// Write image to PNG
[UIImageJPEGRepresentation(screenshot, 1.0) writeToFile:savePath atomically:YES];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
//imageToUpload is a file path with .ig file extension
documentInteractionController = [[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]]retain];
documentInteractionController.UTI = @"com.instagram.exclusivegram";
documentInteractionController.delegate = self;
documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"Insert Caption here" forKey:@"InstagramCaption"];
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:[self appController].view.window animated:YES];
}
}
Hopefully this tutorial was useful but if you have any questions feel free to get in touch, and don’t forget to download Shorts or Not :)
Comments
Powered by Facebook Comments