Swift 2 - 使用UIActivityViewController共享视​​频

时间:2015-11-09 21:30:32

标签: xcode facebook swift2 social uiactivityviewcontroller

我正在尝试使用UIActivityViewController分享来自网址的视频。如果我尝试使用Image,我没有问题。

分享图片作品:

let imageThump = NSURL(string: "https://example.com/image.png")
        let imageData = NSData(contentsOfURL: imageThump!) 
let objectsToShare = [comment!, imageData!]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

分享视频无效。为什么呢?

let videoShare = NSURL(string: "https://example.com/video.mp4")
        let videoData = NSData(contentsOfURL: videoShare!) 
let objectsToShare = [comment!, videoData!]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

3 个答案:

答案 0 :(得分:18)

这就是你做的......

  let urlData = NSData(contentsOfURL: NSURL(string:"https://example.com/video.mov)")!)

            if ((urlData) != nil){

                print(urlData)


                let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
                let docDirectory = paths[0]
                let filePath = "\(docDirectory)/tmpVideo.mov"
                urlData?.writeToFile(filePath, atomically: true)
                // file saved

                let videoLink = NSURL(fileURLWithPath: filePath)


                let objectsToShare = [videoLink] //comment!, imageData!, myWebsite!]
                let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

                activityVC.setValue("Video", forKey: "subject")


                //New Excluded Activities Code
                if #available(iOS 9.0, *) {
                    activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
                } else {
                    // Fallback on earlier versions
                    activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
                }


                self.presentViewController(activityVC, animated: true, completion: nil)
            }

答案 1 :(得分:3)

请记住,视频通常是一个大文件,因此下载和保存视频需要一些时间。在这里,我使用了第一个答案的一部分,但添加了一些GCD。你会得到更好的结果。

我这样做是为了避免应用程序在下载和保存视频之前保持冻结状态:

    let url = NSURL(string:"https://example.com/video.mov)"

    //Show activity indicator

     DispatchQueue.global(qos: .background).async {
         if let urlData = NSData(contentsOf: url){
           let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
              let filePath="\(documentsPath)/tempFile.mov"
              DispatchQueue.main.async {
                urlData.write(toFile: filePath, atomically: true)

                //Hide activity indicator

                let activityVC = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: filePath)], applicationActivities: nil)
                activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact]
                self.present(activityVC, animated: true, completion: nil)
              }
          }
      }

答案 2 :(得分:1)

它无效,因为此类不允许将NSData传递给Facebook活动类型。

  

“UIActivityTypePostToFacebook   该对象将提供的内容发布到Facebook上的用户墙上。

     

使用此服务时,您可以提供NSString,NSAttributedString,UIImage,ALAsset和NSURL对象作为活动项的数据。您还可以指定其内容使用assets-library方案的NSURL对象。“

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivity_Class/index.html#//apple_ref/doc/constant_group/Built_in_Activity_Types

我一直试图让这个也工作。还没弄明白如何让它发挥作用;但是,这就是你的代码无效的原因。