在instagram上发布video使用钩子

我希望我的应用能够将video上传到instagram。

Instagram iPhone Hooks提供了如何使用iPhone挂钩将照片上传到Instagram的信息。 我的问题是,如果任何人有任何经验,如何做到这一点,但一个video?

Instagram的API不直接支持从第三方应用程序上传任何东西。 因此,在向用户提供function时,您必须做一些丑陋的用户体验妥协。

首先,准备要上传到Instagram的video并将其path存储到某处

其次,将其保存到用户的相机胶卷:

if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath)) { UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); } 

第三,现在保存了video,告诉用户,为了将video上传到他们的Instagram,他们必须在点击上传button之后从他们的相机胶卷中select它。

上传button只需执行以下操作:

 NSURL *instagramURL = [NSURL URLWithString:@"instagram://camera"]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { [[UIApplication sharedApplication] openURL:instagramURL]; } 

非常愚蠢的是,Instagram API不支持通过任何API端点进行立即媒体select以进行上传,但是现在,这是唯一的方法。

我有一个类似的问题: Instagram的videoiPhone挂钩 ,我想通了。 有一个未公开的iPhone挂钩,允许您从iPhone照片卷中自动select资源,并为video预加载标题。 这应该给你相同的用户体验,Flipagram应用程序与Instagram分享video。

的Instagram://库AssetPath =资产库%3A%2F%2Fasset%2Fasset.mp4%3Fid%3D8864C466-A45C-4C48-B76F-E3C421711E9D%26ext%3Dmp4&InstagramCaption =有些%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]; } }]; 

尝试与:

 instagram://library?AssetPath=yourVideoPath 

我在这里find了解决办法: http : //blog.horizo​​n.camera/post/102273431070/video-share-objc-ios-instagram

更新了iOS 9。

首先,对于iOS9,您需要添加到Info.plist文件中。 添加一个关键字LSApplicationQueriesSchemes的值为instagram 。 这将白名单的Instagram计划。 更多信息在这里。

这里是基于johnnyg17的工作代码:

 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"); } }]; 

示例instagramURL将是:

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

更新2016/5:请注意,现在不推荐使用ALAssetsLibrary来保存到用户相册,现在已经build议使用相框。

Instagram API的上传function非常有限,特别是当涉及到video文件时。

据我所知,在上传媒体到Instagram时,基本上有两个select。 您可以使用文档交互API将图像传递到Instagram应用程序,也可以调用Instagram相机,并要求用户从他们的相机胶卷中进行select(如Nico所述)。

我很确定你只能通过文件交互系统将JPEG或PNG文件传递给Instagram,所以对于video我相信你现在已经停留在相机胶卷上了。 这绝对不是理想的 – 我现在正在使用的应用程序使用iPhone挂钩,但是我们决定坚持使用图像,直到Instagram改进其API。

这里是在Instagram上分享video的swift代码。

这里videoURL是video的资源url。

  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.") } } } 

确保你在info.plist中添加了下面的代码:

 <key>LSApplicationQueriesSchemes</key> <array> <string>instagram</string> </array> 

Instagram更新了这个使用更新的照片库。 现在,您不必传递图片/videourl,只需传递相应的PHAsset的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]; } 

我用下面的代码,它为我工作。

 ` [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); } }]; } ` 

您可以通过媒体结束点完成

 https://api.instagram.com/v1/media/3?access_token=ACCESS-TOKEN 

获取有关媒体对象的信息。 返回的types键将允许您区分图像和video媒体。

http://instagram.com/developer/endpoints/media/

这里这个链接是为了获取图像媒体ID。 但我希望相同的技术将有助于video。

我在哪里可以find图片的Instagram媒体ID

 NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=315"]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { [[UIApplication sharedApplication] openURL:instagramURL]; } 

优势信息:

  1. instagram://相机将打开相机或照片库(取决于设备),
  2. instagram://应用程序将打开应用程序
  3. instagram:// user?username = foo将打开该用户名
  4. instagram:// location?id = 1将打开该位置
  5. instagram:// media?id = 315将打开该媒体