Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share an image on Instagram in iOS?

My client wants to share an image on Instagram, Twitter, Facebook.

I have done Twitter and Facebook but did not find any API or any thing on internet to share image on Instagram. Is it possible to share image on Instagram? if yes then how?

When I check the developer site of Instagram I have found the Libraries of Ruby on Rails and Python. But there are no documentation of iOS Sdk

I have get token from instagram as per instagram.com/developer but now don't know what to do next step for sharing with instagram image.

like image 653
Hiren Avatar asked Jul 09 '12 10:07

Hiren


People also ask

Why can't I share a photo on Instagram?

The main reason is a bug in the temporary data or cache that is stored on your device. It can be fixed by clearing your cache. If this doesn't work, try restarting your phone, checking the size of the file to see if it's too big, or checking your internet connection and data settings. Check our post for more tips!


2 Answers

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

like image 152
Hiren Avatar answered Sep 22 '22 05:09

Hiren


Here is a full tested code to Upload image + caption text to Instagram..

in.h file

//Instagram @property (nonatomic, retain) UIDocumentInteractionController *documentController;  -(void)instaGramWallPost {             NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];             if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not             {                 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];                 NSLog(@"with File path %@",newJpgPath);                 NSURL *igImageHookFile = [[NSURL alloc]initFileURLWithPath:newJpgPath];                 NSLog(@"url Path %@",igImageHookFile);                  self.documentController.UTI = @"com.instagram.exclusivegram";                 self.documentController = [self setupControllerWithURL:igImageHookFile usingDelegate:self];                 self.documentController=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];                 NSString *caption = @"#Your Text"; //settext as Default Caption                 self.documentController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",caption],@"InstagramCaption", nil];                 [self.documentController presentOpenInMenuFromRect:rect inView: self.view animated:YES];             }             else             {                  NSLog (@"Instagram not found");             } }  - (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {     NSLog(@"file url %@",fileURL);     UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];     interactionController.delegate = interactionDelegate;      return interactionController; } 

OR

-(void)instaGramWallPost {     NSURL *myURL = [NSURL URLWithString:@"Your image url"];     NSData * imageData = [[NSData alloc] initWithContentsOfURL:myURL];     UIImage *imgShare = [[UIImage alloc] initWithData:imageData];      NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];      if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not     {         UIImage *imageToUse = imgShare;         NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];         NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.igo"];         NSData *imageData=UIImagePNGRepresentation(imageToUse);         [imageData writeToFile:saveImagePath atomically:YES];         NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];         self.documentController=[[UIDocumentInteractionController alloc]init];         self.documentController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];         self.documentController.delegate = self;         self.documentController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Testing"], @"InstagramCaption", nil];         self.documentController.UTI = @"com.instagram.exclusivegram";         UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;         [self.documentController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:vc.view animated:YES];     }     else {         DisplayAlertWithTitle(@"Instagram not found", @"")     } } 

and Write this to .plist

<key>LSApplicationQueriesSchemes</key>     <array>         <string>instagram</string>     </array> 
like image 28
Hardik Thakkar Avatar answered Sep 22 '22 05:09

Hardik Thakkar