使用UIActivityViewController分享视频 - 适用于Facebook但不适用于YouTube

时间:2015-09-16 13:18:27

标签: ios objective-c uiactivityviewcontroller

我正在使用原生iOS共享表,它适用于所有内容:邮件,照片,Facebook。除YouTube外的所有内容。我尝试了.mov.mp4扩展名以及不同大小的文件。

更新
带视频的共享表也不显示Twitter选项。照片或相机也是如此。但是,照片的分享表显示了YouTube图标,这告诉我,我做错了。

Image displays iOS app with white share sheet. There's many icons: Mail, Facebook, save video, but not Twitter or YouTube
此状态栏已编辑in the Altershot.

NSData *urlData = [NSData dataWithContentsOfURL:urlToDownload];
if (urlData) {
    // File downloaded

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"file.mov"];
    [urlData writeToFile:filePath atomically:YES];
    // File saved

    NSURL *videoLink = [NSURL fileURLWithPath:filePath];
    NSArray *activityItems = @[videoLink];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [activityViewController setValue:@"Video" forKey:@"subject"];

    if (IS_IPAD) {
        self.popover = [[UIPopoverController alloc]
                        initWithContentViewController:activityViewController];
        [self.popover presentPopoverFromRect:self.shareButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    } else {
        [self presentViewController:activityViewController animated:YES completion:nil];
    }
}

1 个答案:

答案 0 :(得分:2)

据我所知,分享到YouTube并不是开箱即用的。

如果您想将UIActivity选项添加到YouTube,则需要创建自定义UIActivityViewController

简单实施:

//  YouTubeActivity.h
#import <UIKit/UIKit.h>

@interface YouTubeActivity : UIActivity

@end


//  YouTubeActivity.m

#import "YouTubeActivity.h"
//You might need to import some YouTube framework too

@interface YouTubeActivity ()

@end

@implementation YouTubeActivity

- (NSString *)activityType
{
    return @"YouTube";
}

- (NSString *)activityTitle
{
    return @"YouTube";
}

- (UIImage *)_activityImage
{
    return [UIImage imageNamed:@"some_youtube_image"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    return YES;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems
{

}

- (UIViewController *)activityViewController
{
    return nil;
}

- (void)performActivity
{
    // This is where you can do anything you want, and is the whole reason for creating a custom
    // UIActivity

}

@end

创建YouTubeActivity

时,您必须向activityItems添加初始化的UIActivityViewController对象

检查YouTube API Documentation以获取共享功能的实际实现。