【问题标题】:Posting video on instagram using hooks使用钩子在 Instagram 上发布视频
【发布时间】:2013-11-29 19:14:24
【问题描述】:

我希望我的应用能够将视频上传到 Instagram。

Instagram IPhone Hooks 提供了如何使用 iphone 挂钩将照片上传到 instagram 的信息。我的问题是,除了视频之外,是否有人对如何完成相同的操作有任何经验?

【问题讨论】:

  • 来自开发人员支持页面:“确保使用 Instagram 标签标记您的问题,以便从其他开发人员和 Instagram 团队成员那里快速获得答案”。如果 instagram 团队回答了这个问题,那就太好了!
  • iOS 应用 Cinamatic 正在做 Instagram 视频 POST,显然使用 HTTP POST 和 Instagram oAuth

标签: ios objective-c video instagram


【解决方案1】:

Instagram 的 API 不直接支持从第三方应用程序上传任何内容。因此,在向用户提供功能时,您必须在用户体验方面做出一些糟糕的妥协。

首先,准备好要上传到 Instagram 的视频并将其路径存储在某处

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

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

第三,既然视频已保存,请告诉用户为了将视频上传到他们的 Instagram,他们必须在点击上传按钮后从他们的相机胶卷中选择它。

上传按钮只需执行以下操作:

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

Instagram API 不支持通过任何 API 端点立即选择媒体以用于上传目的,这非常愚蠢,但就目前而言,这是唯一的方法。

【讨论】:

  • 虽然公开的文档没有提到它,但确实有一种方法可以启动 Instagram 并预先选择并准备好编辑的照片或视频。长话短说,您可以执行类似instagram://library?AssetPath=[CAMERA ROLL URL]&InstagramCaption=[MESSAGE] 的操作,其中两个参数都必须进行 url 编码。我描述了whole process in our blog here
  • 目前它似乎只有在 Instagram 被终止并重新加载时才有效。如果 Instagram 已经在运行,则将选择上一个视频。在某些情况下,当启用云时,您根本看不到视频。这可能是最新 Instagram 版本中的问题/更改。 Hyperlaps 也经历相同的行为。
  • @Stelabouras:我无法在 Instagram 图片/视频上传页面中加载所选视频
  • 请注意,在 iOS 9 中,您需要将 Instagram 方案列入白名单。看到这个答案:stackoverflow.com/questions/30987986/…
【解决方案2】:

我有一个类似的问题:Instagram Video iPhone Hook 我想通了。有一个未记录的 iPhone 挂钩,可让您自动从 iPhone 相册中选择资产,并为视频预加载标题。这应该为您提供与 Flipagrams 应用程序在将视频分享到 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];
    }
}];

【讨论】:

  • 快速说明:似乎有必要将打开新保存资产的 URL 延迟几秒钟。否则倒数第二个视频将被选中。
  • 太棒了,正是我想要的。奇迹般有效。现在我只需要为 Vine 找到等效的钩子。有什么见解吗?
  • 我找不到任何东西。你知道有什么应用可以钩到葡萄树吗?
  • Flipagram 直接发布到 Vine 没有钩子,但它们必须使用私有 API。我还研究了是否可以以编程方式模拟对 UIDocumentInteractionController 弹出视图内的 Vine 按钮的单击,或者将视图限制为仅显示 Vine,但还没有运气。 (Vine 图标在 UI 层次结构中嵌套了大约 9 层,但它只是一个 UIImageView,而不是 UIButton。)
【解决方案3】:

尝试:

instagram://library?AssetPath=yourVideoPath

我在这里找到了解决方案:http://blog.horizon.camera/post/102273431070/video-share-objc-ios-instagram

【讨论】:

【解决方案4】:

为 iOS 9 更新。

首先,对于 iOS9,您需要将其添加到您的 Info.plist 文件中。添加一个值为instagram 的键a LSApplicationQueriesSchemes。这会将 Instagram 计划列入白名单。 More info here.

这是基于 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&amp;InstagramCaption=Super%20Selfie%20Dance%20%F0%9F%98%83

2016/5 更新:请注意,ALAssetsLibrary 现在已被弃用以保存到用户相册,现在推荐使用 Photos Framework

【讨论】:

  • 我正在使用这个,但你能帮我解决这个问题吗? stackoverflow.com/questions/34226433/…
  • @jose920405 我去看看。顺便说一句,如果我的回答有帮助的话。请给它一个赞成票,以便其他人可以找到它。
  • 感谢崩溃问题已解决,现在我只允许 [NSCharacterSet alphanumericCharacterSet] 解决问题。现在我可以打开 Instagram 应用了。并将视频上传到。
【解决方案5】:

这是在 Instagram 上分享视频的 swift 代码。

这里的videoURL是视频的资产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 应用程序打开了。我可以通过 Instagram 上传视频。
  • 打开 Instagram 应用后应用崩溃。
  • 如果转换为 Swift 4.1 可以使用,但是 Instagram 不再使用标题。
【解决方案6】:

Instagram 已对此进行了更新,以使用更新的照片库。现在,您可以简单地传递相应的 PHAsset 的 localIdentifier,而不是传递图像/视频 URL:

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

【讨论】:

  • 非常感谢。我只是想确认这在今天有效。
【解决方案7】:

Instagram API 的上传功能极其有限,尤其是在视频文件方面。

据我了解,在将媒体上传到 Instagram 时,您基本上有两种选择。您可以使用 Document Interaction API 将图像传递给 Instagram 应用程序,或者您可以调用 Instagram 相机并要求用户从他们的相机胶卷中进行选择(如 Nico 所说)。

我很确定您只能通过文档交互系统将 JPEG 或 PNG 文件传递​​到 Instagram,因此对于视频,我相信您现在无法使用相机胶卷。这绝对不理想 - 我现在正在开发的应用程序使用 iPhone 挂钩,但我们决定坚持使用图片,直到 Instagram 改进他们的 API。

【讨论】:

    【解决方案8】:

    我使用了下面的代码,它对我有用。

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

    【讨论】:

      【解决方案9】:

      你可以通过媒体端点来完成

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

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

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

      此链接用于获取图像媒体 ID。但我希望同样的技术对视频有所帮助。

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

      优势信息:

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

      【讨论】:

      • 感谢您的提示,但实际上我想使用 'iphone hooks' 来实现它。
      • 检查我的答案@beryllium
      • 您是否尝试过按照上述方法将视频上传到 Instagram?我很早就试过了,它只显示黑屏而没有上传。
      • 媒体 ID 仅与已上传到 Instagram 的媒体相关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      相关资源
      最近更新 更多