0

I am trying to share video captured from my app to instagram but app crashes. Photos getting shared successfully.

Code used:

     NSData* videoData = [NSData dataWithContentsOfURL:info[@"UIImagePickerControllerMediaURL"]];
     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"instagram.igo"];
     [videoData writeToFile:path atomically:YES];
      NSURL *fileURL=[NSURL fileURLWithPath:path];

 NSDictionary *fileCaption = @{ @"InstagramCaption" : @"Shared by my app" };

     [self setDocumentInteractionController:[self setupControllerWithURL:fileURL usingDelegate:self]];
            [self.documentInteractionController setUTI:@"com.instagram.exclusivegram"];
            [self.documentInteractionController setAnnotation:fileCaption];
            [self.documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];

On capturing the video, my app redirects and opens Instagram app but immediately it crashes.

Can someone help me with what I am doing wrong or tell me the correct method to share video to Instagram?

halfer
  • 19,824
  • 17
  • 99
  • 186
Monika
  • 23
  • 5

2 Answers2

0

I suspect it's to do with where you've written your video for sharing. In my app I use the following to generate a location:

    NSString *cachesFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [cachesFolder stringByAppendingPathComponent:@"journal.pdf"];
    ... write the file ... 

Then later I share that like this:

    _documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileName]];
    _documentController.delegate = self;
    [_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
brindy
  • 4,585
  • 2
  • 24
  • 27
  • I have already tried NSSearchPathForDirectoriesInDomains but I got same result. – Monika Jun 20 '14 at 06:45
  • I found this answer elsewhere... http://stackoverflow.com/a/21888830/73479 So maybe you can try building a URL with the instagram protocol pointing to the file you've created (though I wouldn't expect it to work with the file stored directly in your app's home directory)? – brindy Jun 20 '14 at 09:42
  • The code on this link will directly open the instagram camera but I want my video to be sent there & video page to be opened as it happens in the case of photo. – Monika Jun 20 '14 at 10:34
0

I did it, I had to save video to the device library, and open camera when Instagram open, then user can choose any video he want from his videos library..

I used this code:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://location?id=1"]]) {

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://camera"];

    NSString  *instagramIgo = [NSTemporaryDirectory() stringByAppendingPathComponent:@"instagram.igo"];
    NSLog(@"video path == %@",instagramIgo);

    // Save video to library ---

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // don't forget to link to the AssetsLibrary framework
    // and also #import <AssetsLibrary/AssetsLibrary.h>

    NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"mp4"];
    NSURL *filePathURL = [NSURL fileURLWithPath:filePathString isDirectory:NO];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:filePathURL completionBlock:^(NSURL *assetURL, NSError *error){
            if (error) {
                // TODO: error handling
            } else {
                // TODO: success handling
            }
        }];
    }
    [library release];


    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    NSURL *fileURL = [NSURL fileURLWithPath:instagramIgo];

    NSLog(@"---- %@",fileURL);

    self.docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    self.docController.delegate = self;
    [self.docController setUTI:@"com.instagram.exclusivegram"];
    [self.docController setAnnotation:@{@"InstagramCaption" : @"Cloud Epic Mar"}];
    [ self.docController presentOpenInMenuFromRect:rect inView:appDelegate.window.rootViewController.view animated:YES];

    [[UIApplication sharedApplication] openURL:instagramURL];
}

I hope that can help you

EDIT : Also, you should check that the video isn't in library before save it.

user306481
  • 135
  • 1
  • 11