In one of my application i have to post image and video on Instagram, so i post here how can we do that in simple way.
First of all we save the image/video and then share it to instagram
for this you can refer this link
whether it is image or video you have to save it to somewhere
- For image you have to store it first in .igo format
- I have used here AFNetworking framework to download video.
- This common function will save your image in application folder and video in photo library as image can be directly share via document picker.
-(void)saveImageorVideo{
UIImage *viewImage;
//i have checked media type you can pass constant as well
switch ([[self.dictPublishConfirm objectForKey:@"media_type"] intValue]) {
case 1 :{
viewImage = imgMain.image;
NSString*path = [self saveImage:viewImage path: [NSString stringWithFormat:@"%@_%@.ig",PATH_IMAGE,self.strAdID]];
strImagePath = [NSURL URLWithString:[[NSString alloc] initWithFormat:@"file://%@", path]];
[self shareToInstagram];
break;
}
case 2:{
NSURL *strVideoPath = [NSURL URLWithString:[self.dictPublishConfirm objectForKey:@"media_video"]];
NSURLRequest *request = [NSURLRequest requestWithURL:strVideoPath];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString*path = [NSString stringWithFormat:@"%@_%@.mp4",PATH_VIDEO,self.strAdID];
videoPath = [NSURL URLWithString:[[NSString alloc] initWithFormat:@"file://%@", path]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)) {
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
break;
}
default:
break;
}
}
//for share to instagram
- (void)shareToInstagram
{
NSURL *instagramURL = [NSURL URLWithString:@"instagram://"];;
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
CGRect rect = CGRectMake(0,0,0,0);
//CGRect cropRect=CGRectMake(0,0,612,612);
if([[self.dictPublishConfirm objectForKey:@"media_type"]isEqualToString:@"1"]){
self.docFile.UTI = @"com.instagram.photo";
self.docFile = [self setupControllerWithURL:strImagePath usingDelegate:self];
self.docFile.annotation = [NSDictionary dictionaryWithObject:@"CatchGram" forKey:@"InstagramCaption"];
[self.docFile presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
} else {
NSURL *instagramURL = [NSURL URLWithString:@"instagram://camera"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
}
}
else
{
NSLog(@"Instagram not installed in this device!\nTo share image please install instagram.");
}
}
//document picker function
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
//share video after it is downloaded
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != nil) {
NSLog(@"Error: %@", error);
}
[self shareToInstagram];
}
Thats it you are done.
Please note
Alternatively, if you want to show only Instagram in the application list (instead of Instagram plus any other public/jpeg-conforming apps) you can specify the extension class igo, which is of type com.instagram.exclusivegram
For video
you can post video this way as well
The undocumented Instagram Video Hook let you share videos to Instagram quite like the Camera Hook
and so you have something like
URL: instagram://library
Path parameter: AssetPath
Comment parameter: InstagramCaption
NSString *instagramURL = [NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",videoURL,[videocaption stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}