从 iOS 将视频分享到 Instagram 源
Posted
技术标签:
【中文标题】从 iOS 将视频分享到 Instagram 源【英文标题】:Share video to Instagram feed from iOS 【发布时间】:2019-04-03 05:02:56 【问题描述】:我一直在尝试为 Instagram 推出的应用创建共享体验,并提供以下两个选项:
Facebook 有一个漂亮的lean documentation 关于它。我使用 UIDocumentInteractionController 尝试了所有可能的排列。我尝试将 uti
com.instagram.photo
和 com.instagram.video
与 ig
扩展一起使用,但我不断获得标准共享弹出窗口,而不是直接启动 Instagram。还尝试了 com.instagram.exclusivegram
和 igo
但这似乎应该触发标准弹出窗口。
最新代码:
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)
【问题讨论】:
有完全相同的问题。我确实设法分享了这样的照片,但我找不到对视频的支持。你也是? 是的,但我见过可以做到这一点的应用程序。不得不处理如此糟糕的文档是多么令人沮丧。 是的。你有Skype吗?也许我们可以一起解决这个问题 @RoiMulia 如果你有同样的问题,也许你可以投票赞成这个问题。 刚做,抱歉没关注上票 【参考方案1】:试试这个:-
我目前正在通过以下方式分享我上次保存的视频:-
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)!)
【讨论】:
经过一天的研究和尝试,这是对我帮助最大的答案!【参考方案2】:进入上述屏幕的唯一方法是首先将视频保存在库中,然后使用未记录的挂钩 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)
【讨论】:
酷,谢谢!我发现如果您将 url 更改为"instagram://library?AssetPath="
,它也可以工作。你有没有想过是否可以传递一个标题字符串?
我发现LocalIdentifier
更稳定。我怀疑这是可能的。希望我错了。
谢谢。我询问了 Facebook 开发人员支持,他们打算什么时候提供可用的文档。他们回答说没有。 (不会在不久的将来)
@dobranoc 在这里也一样。他们还有其他优先事项,比如剥削孩子。【参考方案3】:
- (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.")];
);
【讨论】:
以上是关于从 iOS 将视频分享到 Instagram 源的主要内容,如果未能解决你的问题,请参考以下文章
如何通过链接将视频直接分享到 facebook/instagram 作为可以使用 react-native 直接播放的视频
分享后如何从 Instagram 应用重定向到我的 iOS 应用
在 Android 上将视频和贴纸图片分享到 Instagram Story