0

Thanks to a couple other posts here , I've successfully be able to use the Instagram iPhone hooks to open Instagram and present it with a photo successfully from my application.

(I've made my ViewController class a delegate of UIDocumentInteractionController, and alloc/init'ed a nonatomic/retain property of UIDocumentInteractionController...

However, the key that I put into my NSDictionary that I place in the document controller annotation property will not seem to carry over to Instagram - the caption area is just empty.

How do I deal with this?

Here is my method:

- (void) uploadImageToInstagram:(UIImage *)imageToUpload {
    NSLog(@"postToInstagramInBackground");

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {

        // Upscale to 612x612
        imageToUpload = [self upscaleImage:imageToUpload];

        // Get JPG + IGO Format
        NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/generate.igo"];
        [UIImageJPEGRepresentation(imageToUpload, 1.0) writeToFile:savePath atomically:YES];

        // Paths
        NSURL *imageURL = [NSURL fileURLWithPath:savePath];
        NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@",savePath]];

        // Setup DocController
        self.docController.UTI = @"com.instagram.photo";
        self.docController = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
        self.docController.annotation = [NSDictionary dictionaryWithObject:@"MyApp" forKey:self.caption.text];
        [self.docController setURL:imageURL];
        [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES ];
    }
    else {
        NSLog(@"Instagram not installed in this device!\nTo share image please install Instagram.");
    }
}

There are a few methods that are called here that I haven't included, one that upscales the image to make sure its 612x612, and one setups the file url for the document controller.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jesses.co.tt
  • 2,689
  • 1
  • 30
  • 49
  • 1
    I don't know anythign about posting to instagram, but you are setting the annotation to a dictionary where the object is "MyApp" and the key is the caption text. Don't you need to reverse that? – Eric Jan 09 '14 at 06:40
  • Oh really? I'm not so familiar with NSDictionary, but the examples that I saw online had the name for the object and the caption for he Key... Could be wrong, for sure.. IDK. I just tried to reverse them, and it still didn't work... – jesses.co.tt Jan 09 '14 at 18:41
  • You were right... proper syntax should be the caption as the Object and the Key should be @"InstagramCaption" – jesses.co.tt Jan 09 '14 at 18:56
  • 1
    Glad I could help :)... Was just kind of a shot in the dark. Usually APIs like this will have constants for keys, and your custom data should be in the object. That way it knows exactly what keys to look for in the dictionary – Eric Jan 10 '14 at 18:54

1 Answers1

0

Proper syntax should be

self.docController.annotation = [NSDictionary dictionaryWithObject:self.caption.text forKey:@"InstagramCaption"];
jesses.co.tt
  • 2,689
  • 1
  • 30
  • 49
  • do you know if there are any other keys you can update with this annotation? – noobsmcgoobs May 28 '14 at 01:24
  • 1
    Note that the pre-filled caption feature is no longer supported by Instagram: http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing – T'Pol Feb 19 '18 at 02:30