Manjula Nang
Manjula Nang

Reputation: 21

How to make sure if the video recorded is related to a specific App and not the device's Camera before I delete it Swift

I'm using AVCaptureOutPut to record a video and upload to the server. I'm deleting the last video taken once I upload to the server. Everything works fine. Now I want to delete the video if the user recorded but doesn't want to upload and close the App. I've added same function to the AppDeletage's applicationWillTerminate function but it's picking up the personal video from iCloud if the user goes to the Record screen but like to close the App without recording.

How do I check if the particular video is recorded using the App and not thru the Camera?

Here is my code:

Recording and Saving Video in CaptureView Controller:

 @IBAction func handleMoviewRecord(_ sender: Any) {

        if videoRecordState == .stop {
//            let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
//            let fileUrl = paths[0].appendingPathComponent("output.mov")
//            try? FileManager.default.removeItem(at: fileUrl)

            let tmpdir = NSTemporaryDirectory()
            outputPath = "\(tmpdir)output.mov"
            outputURL = NSURL(fileURLWithPath:outputPath as String)
            if FileManager.default.fileExists(atPath: outputPath) {
                do {
                    try FileManager.default.removeItem(atPath: outputPath)
                } catch _ {
                }
            }
}
}

}

unc fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
            print("FINISHED \(Error.self)")

        // save video to camera roll

              if error == nil {

                if FileManager.default.fileExists(atPath: outputPath){
                        if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.outputPath) {

                UISaveVideoAtPathToSavedPhotosAlbum(outputPath, nil, nil, nil)
                   // outputFileURL.path, nil, nil, nil)
                    UserDefaults.set(.videoPath, to: outputURL.path)
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
                    navigationController?.popViewController(animated: true)
       }
    }
  }
}

App Delegate.Swift

    func applicationWillTerminate(_ application: UIApplication) {
            // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
            // Saves changes in the application's managed object context before the application terminates.
            self.saveContext()
            clearTmpDirectory()

        }

       func clearTmpDirectory() {
             do {
                 let tmpDirURL = FileManager.default.temporaryDirectory
                let filePaths = try FileManager.default.contentsOfDirectory(atPath: "\(tmpDirURL)")
                for filePath in filePaths {
                    try FileManager.default.removeItem(atPath: NSTemporaryDirectory() + filePath)
                }
             } catch {
                //catch the error somehow
             }
         }

Upvotes: 0

Views: 130

Answers (1)

aasatt
aasatt

Reputation: 670

You should not save it to the user's photo library. Just use the temporary file folder and stage your data there before uploading to your server.

let directory = FileManager.default.temporaryDirectory
/// create a new file there and write capture data to this directory

Upvotes: 2

Related Questions