instagram

Posting photo to instagram from your app could be tricky if some basic things isn’t handled properly. I’l be talking about small things where i made mistake. We cannot post image directly to instagram from app/code. Instead we have to use Documentation Interaction API, UIDocumentInteractionController and URL Schemes to open photo on instagram and share/post from there. Here control also won’t be returned back to your app.

The identifier for UTI is “com.instagram.photo”. Before triggering instagram to open your photo, always check if instagram is available on your device:

NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
    //handle
}

Here is the code to open photo on instagram app

-(void) openFileOnInstagram{
	CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();
    NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"InstagramPost.app/no_image.ig"];
    
    NSURL *imageAttachFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", imagePath]];
    
    self.dic.UTI = @"com.instagram.photo";
    self.dic = [self setupControllerWithURL:imageAttachFile usingDelegate:self];
    self.dic=[UIDocumentInteractionController interactionControllerWithURL:imageAttachFile];
    [self.dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id ) interactionDelegate {
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;
    return interactionController;
}

First mistake i did was with getting filepath of my image. My failed attempt was:

NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Document/no_image.ig"];

So i was assuming that device will have same path structure as my macbook have:

/Users/gaanza/Library/Application Support/iPhone Simulator/6.1/Applications/Documents/no_image.ig

It was my mistake, path in device is like this:

/var/mobile/Applications/40591A2D-B464-49FF-8BA2-64CC4886F7A0/InstagramPost.app/no_image.ig

I simply replaced “Documents” with “InstagramPost.app”, InstagramPost is a name of my application so you have to provide yours with .app extension.

Second mistake i did was with file extension of image. You cannot trigger the instagram app to open if we don’t use “.ig” extension, so save the image in PNG/JPEG format but with .ig extension.

Third mistake i did was with not saving file with .ig extension, i was saving file with png extension but using .ig on code call, so this also won’t work :P, you have to save your file in png/jpeg format but with .ig extension.

Please also check instagram reference page.