从iOS将视频分享到Instagram订阅源

p5cysglq  于 2023-01-06  发布在  iOS
关注(0)|答案(3)|浏览(177)

我一直在尝试为我们的应用程序创建一个分享体验,Instagram推出了这两个选项:

Facebook有一个很好的lean documentation。我尝试了所有可能的排列使用UIDocumentInteractionController。我尝试使用uticom.instagram.photocom.instagram.videoig扩展,但我一直得到标准的共享弹出窗口,而不是直接启动Instagram。也尝试了com.instagram.exclusivegramigo,但似乎应该触发标准弹出无论如何。
最新代码:

func shareVideo(_ filePath: String) {
  let url = URL(fileURLWithPath: filePath)
  if(hasInstagram()){
    let newURL = url.deletingPathExtension().appendingPathExtension("ig")
    do {
      try FileManager.default.moveItem(at: url, to: newURL)
    } catch { print(error) }

    let dic = UIDocumentInteractionController(url: newURL)
    dic.uti = "com.instagram.photo"
    dic.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true)
  }
}
yebdmbv4

yebdmbv41#

试试这个。我目前正在分享我最后保存的视频通过这个:

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
if let lastAsset = fetchResult.firstObject {
    let localIdentifier = lastAsset.localIdentifier
    let u = "instagram://library?LocalIdentifier=" + localIdentifier
    let url = NSURL(string: u)!
    if UIApplication.shared.canOpenURL(url as URL) {
        UIApplication.shared.open(URL(string: u)!, options: [:], completionHandler: nil)
    } else {
        
        let urlStr = "https://itunes.apple.com/in/app/instagram/id389801252?mt=8"
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)
            
        } else {
            UIApplication.shared.openURL(URL(string: urlStr)!)
        }
    }
}
vyswwuz2

vyswwuz22#

进入上图所示屏幕的唯一方法是先将视频保存在库中,然后使用未记录的钩子instagram://library传递资产localIdentifier,不要忘记在info.plist中添加instagram查询方案。

if UIApplication.shared.canOpenURL("instagram://app") { // has Instagram
    let url = URL(string: "instagram://library?LocalIdentifier=" + videoLocalIdentifier)

    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler:nil)
    }
}
ljsrvy3e

ljsrvy3e3#

- (void)postMedia:(NSString *)media Type:(BOOL)isVideo {

    [SVProgressHUD showWithStatus:LS(@"Downloading...")];

    //download the file in a seperate thread.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        NSURL *url = [NSURL URLWithString:media];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        if ( urlData ) {

            NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:isVideo?@"instagramShare.mp4":@"instagramShare.jpg"];
            NSURL *outputFileURL = [NSURL URLWithString:filePath];

            dispatch_async(dispatch_get_main_queue(), ^{

                if (![urlData writeToFile:filePath atomically:YES]) {
                    [SVProgressHUD showErrorWithStatus:LS(@"Failed. Please try again.")];
                    return;
                }

                // Check authorization status.
                [PHPhotoLibrary requestAuthorization:^( PHAuthorizationStatus status ) {
                    if ( status == PHAuthorizationStatusAuthorized ) {

                        // Save the movie file to the photo library and cleanup.
                        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                            // In iOS 9 and later, it's possible to move the file into the photo library without duplicating the file data.
                            // This avoids using double the disk space during save, which can make a difference on devices with limited free disk space.                            
                            PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
                            options.shouldMoveFile = YES;
                            PHAssetCreationRequest *changeRequest = [PHAssetCreationRequest creationRequestForAsset];
                            if (isVideo)
                                [changeRequest addResourceWithType:PHAssetResourceTypeVideo fileURL:outputFileURL options:options];
                            else
                                [changeRequest addResourceWithType:PHAssetResourceTypePhoto fileURL:outputFileURL options:options];

                        } completionHandler:^( BOOL success, NSError *error ) {

                            if ( success ) {

                                [SVProgressHUD dismiss];

                                PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
                                fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
                                PHFetchResult *fetchResult;
                                if (isVideo)
                                    fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
                                else
                                    fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
                                PHObject *lastAsset = fetchResult.firstObject;
                                if (lastAsset != nil) {
                                    NSString *localIdentifier = lastAsset.localIdentifier;
                                    NSString *u = [NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@", localIdentifier];
                                    NSURL *url = [NSURL URLWithString:u];
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        if ([[UIApplication sharedApplication] canOpenURL:url]) {
                                            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                                        } else {

                                            NSString *urlStr = @"https://itunes.apple.com/in/app/instagram/id389801252?mt=8";
                                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
                                        }
                                    });
                                }
                            }
                            else {
                                [SVProgressHUD showErrorWithStatus:LS(@"Failed. Please try again.")];
                            }
                        }];
                    }                   
                }];
            });
        }
        else {
            [SVProgressHUD showErrorWithStatus:LS(@"Failed. Please try again.")];
        }
    });
}

相关问题