在iOS 8上将视频分享到Facebook

时间:2014-09-15 19:57:59

标签: ios facebook ios8 uiactivityviewcontroller

我有一些适用于iOS 7的共享代码,但从iOS 8开始,不再有效。

@IBAction func onShareButton(sender: UIButton) {
    let movie = NSBundle.mainBundle().URLForResource("IMG_0564", withExtension: "mp4")!
    let items = [movie]
    let activity = UIActivityViewController(activityItems: items, applicationActivities: nil)
    if activity.respondsToSelector("popoverPresentationController") {
        activity.popoverPresentationController?.sourceView = sender
    }
    self.presentViewController(activity, animated: true, completion: nil)
}

正如我所说,这在iOS 7中运行良好,但是从iOS 8开始,当我选择分享到Facebook时,视频剪辑不再附加到帖子(或在共享面板中可见)。所有其他选项,Mail,Save to Video,AirDrop等都可以正常工作。

我还尝试将这些项目作为AVAssets传递:

    let items = [movie].map { AVAsset.assetWithURL($0) }

和NSData:

    let items = [movie].map { NSData(contentsOfURL: $0) }

这两项都没有对这个问题产生任何影响。

如果我在Objective-C中使用道德等价物,那么问题也会发生,它与语言无关。

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,我发现关键点是文件类型。我试图分享.mp4视频,它不会将视频附加到帖子中。一旦我使用.mov视频,它对我有用。

答案 1 :(得分:1)

好的,我尝试了一种解决方法,它对我有用。

我首先将视频数据保存到文档目录中的文件中,然后附加了该文件。

//write to a file
[videoData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.mov"] atomically:YES];

- (IBAction)ShareVideoWihFacebook:(id)sender
{

    //get the file url
    NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.mov"];

    NSURL *videoURL = [NSURL fileURLWithPath:path];

    UIActivityViewController * activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[videoURL,@"Created by ..."] applicationActivities:NULL];


    [activityVC setExcludedActivityTypes:@[ UIActivityTypeMail,UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePrint, UIActivityTypePostToWeibo,UIActivityTypeMessage,UIActivityTypeAirDrop,UIActivityTypeSaveToCameraRoll]];

    [activityVC setValue:@"My Video" forKey:@"subject"];

    [activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
        //NSLog(@"completed dialog - activity: %@ - finished flag: %d", activityType, completed);
    }];

    [self presentViewController:activityVC animated:TRUE completion:nil];
}