27 Mar 2014

programmatically share image to Instagram through iOS app

Instagram is another most popular social platform for sharing images. Now days, every app share image to Instagram too. Since like other social platforms Instagaram does not provide developers permission to write image directly on the user Instagarm wall. So for this we can use UIDocumentInteractionController provided in Xcode.

For sharing/posting image to Instagarm, you should have two things

1.  Instagram should be installed on your device.
2. User must be logged-in onto Instagram app prior to sharing/Posting image.

NOTE:- If user is not logged in then you have to log him/her in first and then allow him to open your app and re share it.

UIImage *instaImage = [UIImage imageNamed:@"imagetoShare.png"];
    NSString* imagePath = [NSString stringWithFormat:@"%@/image.igo",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYESlastObject]];
    [[NSFileManager defaultManagerremoveItemAtPath:imagePath error:nil];
    [UIImagePNGRepresentation(instaImage) writeToFile:imagePath atomically:YES];
    UIDocumentInteractionController *_docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
    _docController.UTI = @"com.instagram.exclusivegram";

    [_docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];

For sharing image to Instagram, first we created an image with extension .igo(its instagram file extension so that the Instagram app can be shown in UIDocumentController) then save it to our document directory by first removing any image the already written on that location and re writing new image using writeToFile method, that we are going to share to Instagram. Then we will create our UIDocumentController instance and gave it the path of our sharing image that we wrote in document directory. After finish this, we have to specify app identifier to let compiler know which app is capable of opening .igo type document, here we gave "com.instagram.exclusivegram". Lastly, we present our UIDocumentController to user.

Below is the code for above explanation

No comments:

Post a Comment