iOS - Send Image to Instagram - Documentinteraction

How to share an image on Instagram in iOS?

Finally I got the answer. you can not directly post an image on instagram. You have to rediredt your image with UIDocumentInteractionController.

@property (nonatomic, retain) UIDocumentInteractionController *dic;    

CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"];

NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
self.dic.UTI = @"com.instagram.photo";
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 {
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}

NOTE : once you redirect to instagram app you can not back to your app. you have to open your app again

Download source from here

Xamarin iOS post image to Instagram

  1. Change

    Url = NSUrl.FromFilename(path)

    to

    Url = new NSUrl(path,false)

    The second way is pointing to the correct file path not the file name .

  2. Change

    controller.PresentOpenInMenu(CoreGraphics.CGRect.Empty, view, true);

    to

    controller.PresentOptionsMenu(UIScreen.MainScreen.Bounds, (UIApplication.SharedApplication.Delegate as AppDelegate).Window, true);

    Show UIDocumentInteractionController inside current Window and set the proper frame.

UIDocumentInteractionController share via Instagram (and ONLY Instagram)

Turns out to get the doc controller to only open in Instagram you just save your png or jpg file in format ".igo" file type extension, (Maybe stands for Instagram-Only?)

Changed the third line of code in my code to read this instead:

NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.igo"];

(With "igo")

and then it worked! :)



Related Topics



Leave a reply



Submit