天天看點

Instagram分享

分享圖檔

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.

分享視訊

- (void)shareVideoWithInstagram {

[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error) {

NSString *caption = @"Your caption";

    NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",

                                                [[assetURL absoluteString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]],

                                                [caption stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]]

                           ];

    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {

        [[UIApplication sharedApplication] openURL:instagramURL];

    }

}