Share photo to Instagram from my iOS App

10,241

Please try this code in device.... sharing doesn't work in Simulator

It will save image from app to document directory and it will share the same image to instagram.

For sharing i will take .igo format only

-(void)shareInInstagram
{
  NSData *imageData = UIImagePNGRepresentation(imge); //convert image into .png format.

    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it

    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]]; //add our image to the path

    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

    NSLog(@"image saved");


    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();
    NSString *fileNameToSave = [NSString stringWithFormat:@"Documents/insta.igo"];
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNameToSave];
    NSLog(@"jpg path %@",jpgPath);
    NSString *newJpgPath = [NSString stringWithFormat:@"file://%@",jpgPath]; //[[NSString alloc] initWithFormat:@"file://%@", jpgPath] ];
    NSLog(@"with File path %@",newJpgPath);
    NSURL *igImageHookFile = [[NSURL alloc] initFileURLWithPath:newJpgPath];
    NSLog(@"url Path %@",igImageHookFile);

    self.dic.UTI = @"com.instagram.exclusivegram";
    self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
    self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
    [self.dic presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];


}



- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
    NSLog(@"file url %@",fileURL);
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}
Share:
10,241
ayon
Author by

ayon

This is Ashiq. I am a young entrepreneur and Software Engineer from Bangladesh. Expert in Mobile apps development in native Android and iOS platform.

Updated on June 27, 2022

Comments

  • ayon
    ayon almost 2 years

    I have been searching a way to post photo to Instagram from a iOS app I am developing. But seeing some links , it seems to be they don't support write access using their API. So,

    1. Is it possible to share photo to Instagram from iOS by some API or Instagram API ?
    2. If possible can anybody please suggest me a tutorial or documentation to do that ?

    Their api says this...

    "At this time, uploading via the API is not possible. We made a conscious choice not to add this for the following reasons:

    Instagram is about your life on the go – we hope to encourage photos from within the app. However, in the future we may give whitelist access to individual apps on a case by case basis. We want to fight spam & low quality photos. Once we allow uploading from other sources, it's harder to control what comes into the Instagram ecosystem. All this being said, we're working on ways to ensure users have a consistent and high-quality experience on our platform."

    That's why I am looking for any better suggestion.

    Thanks in advance for your help.