I want my app to be able to upload videos to instagram.
Instagram IPhone Hooks gives information how to use the iphone hooks to upload a photo to instagram. My question is if anyone has any experience on how to accomplish the same but for a video?
I want my app to be able to upload videos to instagram.
Instagram IPhone Hooks gives information how to use the iphone hooks to upload a photo to instagram. My question is if anyone has any experience on how to accomplish the same but for a video?
Instagram's API doesn't directly support uploading anything from 3rd party applications. Therefore you have to do some ugly user experience compromises when providing the functionality to your users.
First, Prepare the video you want to upload to Instagram and store the path to it somewhere
Second, Save it to the user's Camera Roll:
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
Third, now that the video is saved, tell the user that in order to upload the video to their Instagram, they must select it from their camera roll after clicking the upload button.
The upload button would simply do the following:
NSURL *instagramURL = [NSURL URLWithString:@"instagram://camera"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
It's very silly that the Instagram API doesn't support immediate media selection through any of the API endpoints for upload purposes, but as it stands right now, this is the only way.
I had a similar question: Instagram Video iPhone Hook and I figured it out. There is an 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.
instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid%3D8864C466-A45C-4C48-B76F-E3C421711E9D%26ext%3Dmp4&InstagramCaption=Some%20Preloaded%20Caption
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];
}
}];
try with:
instagram://library?AssetPath=yourVideoPath
i found the solution here: http://blog.horizon.camera/post/102273431070/video-share-objc-ios-instagram
Updated for iOS 9.
First, for iOS9 you'll need to add to your Info.plist
file. Add a key a LSApplicationQueriesSchemes
with the value instagram
. This will whitelist the Instagram scheme. More info here.
Here is working code based on johnnyg17's:
NSString *moviePath = @"<# /path/to/movie #>";
NSString *caption = @"<# Your caption #>";
NSURL *movieURL = [NSURL fileURLWithPath:moviePath isDirectory:NO];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:movieURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
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];
}
else {
NSLog(@"Can't open Instagram");
}
}];
A sample instagramURL would be:
instagram://library?AssetPath=assets%2Dlibrary%3A%2F%2Fasset%2Fasset%2Emov%3Fid%3D69920271%2D2D44%2D4A84%2DA373%2D13602E8910B6%26ext%3Dmov&InstagramCaption=Super%20Selfie%20Dance%20%F0%9F%98%83
Update 2016/5: Note that ALAssetsLibrary
is now deprecated for saving to users photo album, and the Photos Framework is now reccomended.
Here is swift code for share video on Instagram.
here videoURL is asset url of video.
func shareVideoToInstagram()
{
let videoURL : NSURL = "URL of video"
let library = ALAssetsLibrary()
library.writeVideoAtPathToSavedPhotosAlbum(videoURL) { (newURL, error) in
let caption = "write your caption here..."
let instagramString = "instagram://library?AssetPath=\((newURL.absoluteString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet()))!)&InstagramCaption=\((caption.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet()))!)"
let instagramURL = NSURL(string: instagramString)
if UIApplication.sharedApplication().canOpenURL(instagramURL!)
{
UIApplication.sharedApplication().openURL(instagramURL!)
}
else
{
print("Instagram app not installed.")
}
}
}
Make sure that you have added below code in info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
Instagram has updated this to use the newer Photos Library. Now, instead of passing the image/videos URL, you can simply pass the corresponding PHAsset's localIdentifier:
PHAsset *first = /* Some PHAsset that you want to open Instagram to */;
NSURL *instagramURL = [NSURL URLWithString:[@"instagram://library?AssetPath=" stringByAppendingString:first.localIdentifier]];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
The Instagram API is extremely limited in its upload functionality, particularly when it comes to video files.
From what I understand, you basically have two options when it comes to uploading media to Instagram. You can either use the Document Interaction API to pass an image over to the Instagram app, or you can call up the Instagram camera and ask the user to choose from their camera roll (as Nico said).
I'm pretty sure you can only pass JPEG or PNG files to Instagram through the Document Interaction system, so for video I believe you're stuck with the camera roll for now. It's definitely not ideal - the app I'm working on right now uses iPhone hooks, but we've decided to stick with images until Instagram improves their API.
I have used below code and it's working for me.
` [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized: {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://"]])
{
[MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleExpand];
[MMProgressHUD showWithTitle:APPNAME status:@"Please wait..."];
_FinalVideoPath = [_FinalVideoPath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", _FinalVideoPath]];
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
dispatch_async(dispatch_get_main_queue(), ^{
// Write it to cache directory
NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
[videoData writeToFile:videoPath atomically:YES];
[self createAlbumInPhotosLibrary:APPNAME videoAtFile:[NSURL fileURLWithPath:videoPath]ShareOnString:@"Instagram"];
});
});
}
else
{
[MMProgressHUD dismiss];
[STMethod showAlert:self Title:APPNAME Message:@"Please install Instagram to share this video" ButtonTitle:@"Ok"];
}
break;
}
case PHAuthorizationStatusRestricted: {
[self PhotosDenied];
break;
}
case PHAuthorizationStatusDenied: {
[self PhotosDenied];
break;
}
default:
{
break;
}
}
}];
- (void)createAlbumInPhotosLibrary:(NSString *)photoAlbumName videoAtFile:(NSURL *)videoURL ShareOnString:(NSString*)ShareOnStr
{
// RELIVIT_moments
__block PHFetchResult *photosAsset;
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;
// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", photoAlbumName];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAny
options:fetchOptions].firstObject;
// Create the album
if (!collection)
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:photoAlbumName];
placeholder = [createAlbum placeholderForCreatedAssetCollection];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier]
options:nil];
collection = collectionFetchResult.firstObject;
[self saveVideoInRelivitFolderSetPlaceHolder:placeholder photosAsset:photosAsset collection:collection VideoAtFile:videoURL ShareOnStr:ShareOnStr];
}
else
{
[MMProgressHUD dismiss];
}
}];
} else {
[self saveVideoInRelivitFolderSetPlaceHolder:placeholder photosAsset:photosAsset collection:collection VideoAtFile:videoURL ShareOnStr:ShareOnStr];
}
}
- (void)saveVideoInRelivitFolderSetPlaceHolder:(PHObjectPlaceholder *)placeholderLocal photosAsset:(PHFetchResult *)photosAssetLocal collection:(PHAssetCollection *)collectionLocal VideoAtFile:(NSURL *)videoURL ShareOnStr:(NSString*)ShareOnstring
{
__block PHFetchResult *photosAsset = photosAssetLocal;
__block PHAssetCollection *collection = collectionLocal;
__block PHObjectPlaceholder *placeholder = placeholderLocal;
// Save to the album
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL];
placeholder = [assetRequest placeholderForCreatedAsset];
photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
assets:photosAsset];
[albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
NSLog(@"done");
NSString *LocalIdentifire=placeholder.localIdentifier;
NSString *AssetIdentifire=[LocalIdentifire stringByReplacingOccurrencesOfString:@"/.*" withString:@""];
NSString *Extension=@"mov";
NSString *AssetURL=[NSString stringWithFormat:@"assets-library://asset/asset.%@?id=%@&ext=%@",Extension,AssetIdentifire,Extension];
NSURL *aSSurl=[NSURL URLWithString:AssetURL];
[MMProgressHUD dismiss];
if ([ShareOnstring isEqualToString:@"Instagram"])
{
NSLog(@"%@",AssetURL);
NSString *caption = @"#Zoetrope";
NSURL *instagramURL = [NSURL URLWithString:
[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",
[[aSSurl absoluteString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]],
[caption stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]]
];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
[MMProgressHUD dismiss];
[[UIApplication sharedApplication] openURL:instagramURL];
}
else
{
NSLog(@"Can't open Instagram");
[MMProgressHUD dismiss];
[STMethod showAlert:self Title:APPNAME Message:@"Please install Instagram to share this video" ButtonTitle:@"Ok"];
}
}
else
{
NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:videoPath] error:&removeError];
NSLog(@"%@",[removeError localizedDescription]);
ZShareSuccessViewController *ShareView=[self.storyboard instantiateViewControllerWithIdentifier:@"ZShareSuccessViewController"];
[self.navigationController pushViewController:ShareView animated:true];
}
}
else
{
if (![ShareOnstring isEqualToString:@"Instagram"] || [ShareOnstring isEqualToString:@"facebook"])
{
[self PhotosDenied];
}
[MMProgressHUD dismiss];
NSLog(@"%@", error.localizedDescription);
}
}];
}
`
you can done by media end point
https://api.instagram.com/v1/media/3?access_token=ACCESS-TOKEN
Get information about a media object. The returned type key will allow you to differentiate between image and video media.
http://instagram.com/developer/endpoints/media/
Here this link is for get image media id. but i hope same technique will have help in video.
Where do I find the Instagram media ID of a image
NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=315"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
Advantage info: