2

The current best solution:

https://stackoverflow.com/a/21888830/1786820

I am trying to do one better, by opening the Instagram app with a preselected video file from the PhotoRoll, and a preloaded caption. In the Flipagram app, they do just this. When you hit share on a video, they save it your camera roll, suggest a caption, then direct to the Instagram app photo selection screen, with the video preselected. Even if the video is not the latest media in the PhotoRoll, it correctly highlights the correct video, along the caption prepared in the Flipagram app.

Is this possibly an undocumented iPhone hook?

Any help is appreciated.

Community
  • 1
  • 1
johnnyg17
  • 610
  • 6
  • 14

2 Answers2

13

I came up with the idea to allow my app to accept the instagram:// URL schema. The hook from Flipagram opened up in my app as the following:

instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid%3D8864C466-A45C-4C48-B76F-E3C421711E9D%26ext%3Dmp4&InstagramCaption=Some%20Preloaded%20Caption

The undocumented iPhone hook that allows you to automatically select assets from the iPhones photo roll, and preload a caption for the video. This should give you the same user experience that Flipagrams app has with sharing a video to Instagram.

NSURL *videoFilePath = ...; // Your local path to the video
NSString *caption = @"Some Preloaded Caption";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoFilePath] completionBlock:^(NSURL *assetURL, NSError *error) {
    NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",[assetURL absoluteString].percentEscape,caption.percentEscape]];
    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
        [[UIApplication sharedApplication] openURL:instagramURL];
    }
}];

Works great!

Update: Instagram has removed the ability to pass the caption to their app. The best solution now it to just copy the desired caption to the paste board.

johnnyg17
  • 610
  • 6
  • 14
  • 1
    Quick Note: seems like it is necessary to delay the opening of the URL of newly saved assets for a few seconds. otherwise the second to last video is the one selected. – johnnyg17 Oct 15 '14 at 00:01
  • if i wish to add captions then this is not works for me so any other option to add caption with video file to share on instagram? – Ashish Thakkar Jan 27 '15 at 12:17
  • @Ashish you might want to make sure you are url encoding your caption before you build the url to send to Instagram. – johnnyg17 Jan 27 '15 at 19:44
  • yes i am sending url in encoded format but also i have to encode the caption to send it to Instagram ? – Ashish Thakkar Jan 28 '15 at 04:49
  • None of the item shows selected, what may be the problem? – Mehul Thakkar Dec 23 '15 at 09:55
  • error http://stackoverflow.com/questions/34226433/instagram-hooks-pre-select-media-issue – jose920405 Dec 23 '15 at 17:22
  • Confirmed. This works. To just open photo library, use this: instagram://library?AssetPath=assets-library – sabiland Jan 13 '16 at 11:37
  • 1
    for **IOS 9** the `stringByAddingPercentEscapesUsingEncoding` is depecrated, so you will have to use `stringByAddingPercentEncodingWithAllowedCharacters`, but neither `URLPathAllowedCharacterSet()` nor `alphanumericCharacterSet()` is enough to produce the same output as above. Solution is appending `alphanumericCharacterSet()` like: `let characterSet = NSMutableCharacterSet.alphanumericCharacterSet() characterSet.addCharactersInString("-.") //add dash and period to list` – Dan Apr 13 '16 at 13:05
0

The answer is that it is not pulling the video from the camera roll at all, it might just look like it is.

Documentation here: http://instagram.com/developer/mobile-sharing/iphone-hooks/

The relevant bit is the bottom section "Document Interaction".

You would do this by doing something like this:

NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"instagram.igo"];
NSData *data = // set this yourself

NSError *error = nil;
if (! [data writeToFile:filePath options:NSDataWritingAtomic error:&error])
{
    // error here
}

self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.documentInteractionController.delegate = self;
self.documentInteractionController.UTI = @"com.instagram.exclusivegram";
self.documentInteractionController.annotation = @{ @"InstagramCaption" : @"caption text here" };
const BOOL couldOpen = [self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:myView animated:YES];

Set the data, the caption, and the view to present from yourself. Notice the UIDocumentInteractionController is also a property. It should be retained somewhere and not just a local variable in a method because it needs to exist outside of that scope when the method completes.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • This seems to work great for images, but not videos. I just get a black image. My guess it is an iPhone Hook, because the document interaction view never appears in Flipagrams app. – johnnyg17 Oct 14 '14 at 22:13