Vaibhav Jhaveri
Vaibhav Jhaveri

Reputation: 1605

Upload Image/Video from Photos to Instagram using iPhone Hooks

I want to upload Image and Video to Instagram using iPhone Hooks but unable to achieve the result.

For Video, I use following code :

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeVideoAtPathToSavedPhotosAlbum:videoFilePath completionBlock:^(NSURL *assetURL, NSError *error)
{
    NSString *CFURL = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[NSString stringWithFormat:@"%@", [assetURL absoluteString]], NULL, CFSTR("!$&'()*+,-./:;=?@_~"), kCFStringEncodingUTF8));

    NSString *CFCaption = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[NSString stringWithFormat:@"%@", strInstagramCaption], NULL, CFSTR("!$&'()*+,-./:;=?@_~"), kCFStringEncodingUTF8));

    NSString *instagramString = [NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@", [assetURL absoluteString], strInstagramCaption];
    NSLog(@"instagramString : %@", instagramString);

    NSURL *instagramURL = [NSURL URLWithString:instagramString];
    NSLog(@"instagramURL : %@", instagramURL);

    if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
    {
        [[UIApplication sharedApplication] openURL:instagramURL];
    }

}];

Upvotes: 1

Views: 516

Answers (2)

Nimit Parekh
Nimit Parekh

Reputation: 16864

Here is code for share image on instagram using URI.

@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;
}

Also you can download the sample project from here.

Upvotes: 1

Vizllx
Vizllx

Reputation: 9246

This is the way how you should convert asset path to valid NSUrl:-

NSURL *instagramURL = [NSURL URLWithString:[[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",[assetURL absoluteString],caption]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Upvotes: 0

Related Questions