Swift 2 - Share video with UIActivityViewController

吃可爱长大的小学妹 提交于 2019-12-13 13:04:27

问题


I am trying to share a video from a URL using UIActivityViewController. If I try with an Image I have not problems.

Share Image Works:

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)

Share Video is not working. Why?

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)

回答1:


THIS IS HOW YOU DO IT....

  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)
            }



回答2:


Remember that a video is usually a big file, so it'll take some time for it to be downloaded and saved. Here I used part of the first answer but adding some GCD. You'll get better results.

This is how I did it to avoid the App to stay frozen until the video is downloaded and saved:

    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)
              }
          }
      }



回答3:


It is not working because this class does not allow NSData to be passed to the Facebook activity type.

"UIActivityTypePostToFacebook The object posts the provided content to the user’s wall on Facebook.

When using this service, you can provide NSString, NSAttributedString, UIImage, ALAsset, and NSURL objects as data for the activity items. You may also specify NSURL objects whose contents use the assets-library scheme."

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

I have been trying to get this to work as well. Haven't figured out how to get it to work; however, this is why your code is not working.



来源:https://stackoverflow.com/questions/33618673/swift-2-share-video-with-uiactivityviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!