29

Instagram recently made a change to their API policy which allowed developers to post pictures to the Instagram platform via their own app. Several other techniques we're previously employed to do this. One of them was to invoke the Instagram application which would essentially open up Instagram and do the sharing from there. A tutorial on how this can be done can be seen here: How to share image to Instagram from your own iOS app

However there are several applications out there that allow for direct sharing to the Instagram platform without invoking the Instagram application. Hipstamatic's Oggl allows for direct sharing to Instagram without invoking Instagram. Below I have posted some screen shots of the process.

Once my picture was taken, Oggl gave me several other social networks to which I could share my photo to. I've selected Facebook and Instagram.

enter image description here

After I selected Instagram, it opened up Safari and it brought me to the following two pages to authorize Oggl to post to Instagram. I entered in my Instagram credentials and then it brought me to the authorization page.

enter image description here enter image description here

Once I authorized Oggl, I was able to upload to Instagram and within seconds, I saw the photo on my Instagram news feed. This type of sharing is very analogous to Facebook and Twitter sharing. It has the same concept. How can one go about doing this? How can one replicate this exact process in their app? The pictures taken in my application are 612px by 612 px, so they are compatible with the dimensions for photos taken on Instagram. I've already implemented sharing to Facebook and Twitter but I would like to implement uploading to Instagram just like how Oggl did. Is this possible?

There are many iOS developers out there who can benefit from a well detailed canonical answer to this question.

Thank You

deadlock
  • 7,048
  • 14
  • 67
  • 115
  • 3
    I don't see where you have actually asked a question. – Jason Aug 14 '13 at 21:27
  • Hi @noahandthewhale did you implemented this feauture. I want same this. can you please suggest me. – Kittu Apr 15 '15 at 07:03
  • @Kittu, what we ended up doing was invoking the Instagram app and passing our media through there. It kinda sucks :( because users have to leave our app in order to post to Instagram and there is no way for them to come back into our app. For direct sharing like above, you need to gain whitelist access to post. Which means you either need to be good friends with the folks over at Instagram or wait until they release a public api for posting directly. In the meantime, follow the documentation here: https://instagram.com/developer/mobile-sharing/ – deadlock Apr 17 '15 at 21:20
  • This QA is a bit old. You certainly SHOULD NOT try to directly post to things like Instagram. It's very non-Apple. (It's stupid that Instagram<->Hipsta do this ... in the future they will stop it.) You just launch a `UIActivityViewController` with a line of code and that's it. – Fattie Nov 06 '16 at 16:28

8 Answers8

27

Hipstamatic are one of the few apps that has been given write access via Instagram's API.

Instagram doesn't have a public API for posting photos, it lets you get their data but not write your own.

Hipstamatic and several other apps have negotiated special deals with Instagram that allow them to use the write API and post photos directly.

For all other developers you need to use iOS hooks to share to Instagram by switching to their app.

As far as I know its not easy to get Instagram to allow you write access- you would need to have a top app with high quality content and be friendly with the right people :)

Rowan
  • 616
  • 7
  • 5
  • 5
    Use `UIDocumentInteractionController` to launch Instagram app with your chosen photo AND caption. (User can edit the caption once inside the instagram app however). – Albert Renshaw Feb 04 '14 at 21:17
8

You can not directly post an image on Instagram. You have to redirect your image with UIDocumentInteractionController. Use Below code , Hope It will help

@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;
}
Sport
  • 8,570
  • 6
  • 46
  • 65
Faran Ghani
  • 277
  • 4
  • 17
  • 3
    This code snippet seems incomplete and contradictory. self.dic is assigned twice, and the UTI property is assigned before dic is defined. How is the image itself used? Presumably you're generating an image from the current view, but that code is missing as well. – EPage_Ed Jul 21 '15 at 22:11
4

What you are trying to do is implemented through the Instagram API http://instagram.com/developer/. You can also use the Instagram App to do some similar actions. These are documented under iPhone Hooks. If you are using Ruby Motion, then you will be able to use the official framework. Unfortunately, there is no officially supported Objective-C iOS API but some open source alternatives are available, like the NRGramKit.

The exact way of implementing the interaction with the Instagram API is beyond a Stack Overflow answer but the links above should give you a good starting point if you are familiar with iOS programming.

allprog
  • 16,540
  • 9
  • 56
  • 97
  • thanks for the reply, I'm going to look into it right now and let you know about my progress. – deadlock Aug 15 '13 at 17:57
  • This is a quite advanced app so I suggest you implement it piece by piece. Don't try to *attack* the whole thing at once. :) If you feel like this solved your issue, I'd appreciate you accept this answer. – allprog Aug 15 '13 at 19:32
4

verbatim from the Instagram API documentation:

https://instagram.com/developer/endpoints/media/

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

  1. Instagram is about your life on the go – we hope to encourage photos from within the app.
  2. 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.
Community
  • 1
  • 1
NiloVelez
  • 3,611
  • 1
  • 22
  • 30
4

if you want to just send the image without clipping you should set image size of 640 x 640 or the application crash

This my swift code

 var instagramURL = NSURL(string: "instagram://app")!
        if UIApplication.sharedApplication().canOpenURL(instagramURL) {
            var documentDirectory = NSHomeDirectory().stringByAppendingPathComponent("Documents")
            var saveImagePath = documentDirectory.stringByAppendingPathComponent("Image.igo")
            var imageData = UIImagePNGRepresentation(self.bestScoreShareView.takeSnapshot(W: 640, H: 640))
            imageData.writeToFile(saveImagePath, atomically: true)
            var imageURL = NSURL.fileURLWithPath(saveImagePath)!
           docController  = UIDocumentInteractionController()
            docController.delegate = self
            docController.UTI = "com.instagram.exclusivegram"
            docController.URL = imageURL
            docController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)

        } else {
            println("instagram not found")
        }

extension for take shapshot from uiview

extension UIView {
    func takeSnapshot(#W: CGFloat, H: CGFloat) -> UIImage {
        var cropRect = CGRectMake(0, 0, 600, 600)
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
        drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        image.imageWithNewSize(CGSizeMake(W, H))
        return image
    }
}

extension for set image size

extension UIImage {
     func imageWithNewSize(newSize:CGSize) ->UIImage {
        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))

        let newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        return newImage
    }
}
Beslan Tularov
  • 3,111
  • 1
  • 21
  • 34
  • in your set image size extension `newImage` is optional but you are returning a non optional instance of UIImage. `let newImage = UIGraphicsGetImageFromCurrentImageContext()!` would work – Tucker Sherman Nov 18 '16 at 19:02
1

You may also want to check out the "Document Interaction" section of Instagram's API documentation for iPhone Hooks. This could be leveraged to do what you are trying to do and it might be the way Oggl does it.

omdel
  • 911
  • 1
  • 9
  • 13
0

I have used below code for sharing photo in Instagram.

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
    if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
    {
        NSData *imageData = UIImagePNGRepresentation(image); //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
    {
        UIAlertView *errMsg = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"No Instagram Available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [errMsg show];
    }
    
Nirzar Gandhi
  • 79
  • 1
  • 8
0

You can simply use UIActivityViewController as well,

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://app"]]) //check for App is install or not
    {
        NSArray * activityItems = [NSArray arrayWithObjects:newImage, nil];
        UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        activityController.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, nil];
        
        [self presentViewController:activityController animated:YES completion:nil];
        
        //after sent
        [activityController setCompletionWithItemsHandler:^(NSString *act, BOOL done, NSArray *returnedItems, NSError *activityError){
            
            if (done){
                
                UIAlertController *alert;
                UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
                    [alert dismissViewControllerAnimated:YES completion:nil];
                }];
                
                alert = [UIAlertController alertControllerWithTitle:@"Successfully Posted!" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction: ok];
                [self presentViewController:alert animated:YES completion:nil];
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }];
}
Mona
  • 5,939
  • 3
  • 28
  • 33