title: How to convert a downloaded NSData type to a video type to be played on iPhone Swift?
body: <p>I am downloading a <code>NSData</code> file from a chat server, and I know that this file is uploaded normally as a video file. I would like to get this file and display it as a video Safari. I think that in order to do that I cannot display it as an <code>NSData</code>, is that right? if not, what is the format that I can have this file in in order to display it in my iPhone? </p>
  
  <p>so I started to think about converting it back again to a video file, and here is what I used based on <a href="https://stackoverflow.com/questions/35145262/how-to-convert-video-data-to-nsurl">this answer: How to convert video data to NSURL</a></p>
  
  <p>The answer explains that we can convert the video to a <code>URL</code> to use it to play the video later. I have tried the solution and got the <code>URL</code>. However the url of type string that I get could not be played on Safari so I assumed I might be doing something wrong. I am supposing that the error might come from the way I use encoding, as I am not sure about what to put in it.</p>
  
  <p>Please note that <code>filePath</code> is the one containing the path to the <code>NSData</code> file.</p>
  
  <pre><code>            let filePath = self.documentsPathForFileName(name: "video.mov")
  
  
              do { try [videomessage?.url.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)]}
              catch{
                  print ("error")
              }
              let videoFileURL = NSURL(fileURLWithPath: filePath)
  
              print (videoFileURL)
  
  func documentsPathForFileName(name: String) -&gt; String {
  
      let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
      return documentsPath + name
  }
  </code></pre>